node js

Node.js is a versatile runtime environment that encompasses everything necessary for executing JavaScript programs. It finds extensive use in running server-side scripts to render content before delivering it to web browsers. In tandem with Node.js, there’s NPM (Node Package Manager), an application and repository used for the development and sharing of JavaScript code. This guide aims to walk you through the process of installing and updating Node.js and NPM on a Windows system while also delving into essential Node.js commands.

Prerequisites

Before we embark on the installation journey, ensure you have the following:

  1. User Account with Administrator Privileges: You’ll need this for software installation.
  2. Access to the Windows Command Line or PowerShell: Access these tools by searching for “cmd” or “Powershell” in the Windows menu, right-clicking, and choosing to run as an administrator.

Installing Node.js and NPM on Windows

Step 1: Download Node.js Installer

  1. Open your web browser and navigate to the official Node.js website at https://nodejs.org/en/download/.
  2. Click the “Windows Installer” button to download the latest default version. As of the time of writing, the latest version was 10.16.0-x64, and it includes the NPM package manager.

Step 2: Install Node.js and NPM from the Browser

  1. Once the installer finishes downloading, locate the downloaded file, and either click on the file in your browser’s download section or double-click it in the saved location to launch the installation process.
  2. Windows may prompt you to confirm if you want to run the software – click “Run” to proceed.
  3. The Node.js Setup Wizard will welcome you – click “Next” to continue.
  4. Review the license agreement on the next screen. If you agree to the terms, click “Next” to initiate the installation.
  5. You’ll be prompted to select the installation location. Unless you have a specific reason to choose another location, stick with the default and click “Next.”
  6. The wizard offers the option to select components to include or remove from the installation. Unless you have specific requirements, it’s recommended to accept the defaults by clicking “Next.”
  7. Finally, click the “Install” button to commence the installation process. Once it’s complete, click “Finish.”

Step 3: Verify Installation

To ensure a successful installation, open a command prompt or PowerShell and enter the following commands:

node -v

This command will display the version of Node.js installed on your system. You can also verify the NPM version:

npm -v

Updating Node.js and NPM on Windows

The simplest way to update Node.js and NPM is to download the latest version of the software. On the Node.js download page, you’ll find the latest version listed right below the Windows Installer link. You can compare this version to the one you have installed.

To perform the update, download the installer for the latest version and run it. The setup wizard will automatically replace the old version with the new one.

Uninstalling Node.js and NPM on Windows

If the need arises to uninstall Node.js and NPM, follow these steps:

  1. Click the “Start” button, go to “Settings” (the gear icon), and select “Apps.”
  2. Scroll down to find “Node.js” and click on it to highlight it.
  3. Choose “Uninstall.” This action launches a wizard to guide you through the uninstallation process.

Basic Node.js Usage

Node.js functions as a framework and interprets the commands you provide. To test your Node.js installation, create a simple Hello World script:

  1. Open a text editor of your choice.
  2. Copy and paste the following code into the text editor:
javascript
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('Hello World!');
}).listen(8080);
  1. Save the file and exit. Open PowerShell and enter the following command to run the script:
shell
node \users\<your_username>\myprogram.js

Although it may appear as if nothing has happened, your script is running in the background. You might encounter a Windows Defender notice about allowing traffic; in this case, click “Allow.”

  1. Open a web browser and enter the following in the address bar:
url
http://localhost:8080

You should see the text “Hello World!” in the upper-left corner. Your computer is now acting as a server, and any other device attempting to access your system on port 8080 will encounter the “Hello World” message.

To stop the program, switch back to PowerShell and press Ctrl+C. The system will revert to a command prompt, and you can close the window at your convenience.

Conclusion

You’ve successfully installed Node.js and the NPM package manager, and even created your first Node.js JavaScript program. The NPM framework provides access to a wide array of JavaScript solutions, which you can explore at npmjs.com. Enjoy your journey into the world of Node.js development!

© 2013 - 2024 Foreignerds. All Rights Reserved

facebookFacebook
twitterTwitter
linkedinLinkedin
instagramInstagram
whatsapp
support