• Home
  • Testing
  • SAP
  • Web
  • Must Learn!
  • Big Data
  • Live Projects
  • Blog

To start building your Node.js applications, the first step is the installation of the node.js framework. The Node.js framework is available for a variety of operating systems right from Windows to Ubuntu and OS X. Once the Node.js framework is installed you can start building your first Node.js applications.

Node.js also has the ability to embedded external functionality or extended functionality by making use of custom modules. These modules have to be installed separately. An example of a module is the MongoDB module which allows you to work with MongoDB databases from your Node.js application.

In this tutorial, you will learn-

How to install node.js

The first steps in using Node.js is the installation of the Node.js libraries on the client system. To perform the installation of Node.js, perform the below steps;

Step 1) Go to the site https://nodejs.org/en/download/ and download the necessary binary files. In our example, we are going to the download the 32-bit setup files for Node.js.

How to Download & Install Node.js - NPM on Windows

Step 2) Double click on the downloaded .msi file to start the installation. Click the Run button in the first screen to begin the installation.

How to Download & Install Node.js - NPM on Windows

Step 3) In the next screen, click the "Next" button to continue with the installation

How to Download & Install Node.js - NPM on Windows

Step 4) In the next screen Accept the license agreement and click on the Next button.

How to Download & Install Node.js - NPM on Windows

Step 5) In the next screen, choose the location where Node.js needs to be installed and then click on the Next button.

  1. First enter the file location for the installation of Node.js. This is where the files for Node.js will be stored after the installation.
  2. Click on the Next button to proceed ahead with the installation.

How to Download & Install Node.js - NPM on Windows

Step 6) Accept the default components and click on the next button.

How to Download & Install Node.js - NPM on Windows

Step 7) In the next screen, click the Install button to start the installation.

How to Download & Install Node.js - NPM on Windows

Step 8) Click the Finish button to complete the installation.

How to Download & Install Node.js - NPM on Windows

Installing NPM (Node Package Manager) on Windows

The other way to install Node.js on any client machine is to use a "package manager".

On windows, the node package manager is known as Chocolatey. It was designed to be a decentralized framework for quickly installing applications and tools that you need.

To install Node.js via Chocolatey, the following steps need to be performed.

Step 1) Installing Chocolatey – The Chocolatey website (https://chocolatey.org/) has very clear instructions on how this framework needs to be installed.

  • The first steps is to run the below command in the command prompt windows. This command is taken from the Chocolatey web site and is the standard command for installing Node.js via Chocolatey.
  • The below command is a PowerShell command which calls the remote PowerShell script on the Chocolatey website. This command needs to be run in a PowerShell command window.
  • This PowerShell script does all the necessary work of downloading the required components and installing them accordingly.

@powershell -NoProfile -ExecutionPolicy Bypass -Command "iex ((new-object wet.webclient).DownloadString('https://chocolatey.org/install.ps1'))" && SET PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin

How to Download & Install Node.js - NPM on Windows

Step 2) The next step is to install Node.js to your local machine using the Chocolatey, package manager. This can be done by running the below command in the command prompt.

cinst nodejs install

How to Download & Install Node.js - NPM on Windows

If the installation is successful, you will get the message of the successful installation of Node.js.

Note:If you get an error like "C:\ProgramData\chocolatey\lib\libreoffice\tools\chocolateyInstall.ps1" Then manually create the folder in the path

Running your first Hello world application in Node.js

Once you have downloaded and installed Node.js on your computer, lets try to display "Hello World" in a web browser.

Create file Node.js with file name firstprogram.js

var http = require('http');

http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.end('Hello World!');
}).listen(8080);

Code Explanation:

  1. The basic functionality of the "require" function is that it reads a JavaScript file, executes the file, and then proceeds to return an object. Using this object, one can then use the various functionalities available in the module called by the require function. So in our case, since we want to use the functionality of http and we are using the require(http) command.
  2. In this 2nd line of code, we are creating a server application which is based on a simple function. This function is called, whenever a request is made to our server application.
  3. When a request is received, we are asking our function to return a "Hello World" response to the client. The writeHead function is used to send header data to the client and while the end function will close the connection to the client.
  4. We are then using the server.listen function to make our server application listen to client requests on port no 7000. You can specify any available port over here.

Executing the code

Step 1) Save the file on your computer: C:\Users\Your Name\ firstprogram.js

Step 2) In the command prompt, navigate to the folder where the file is stored. Enter the command Node firstprogram.js

How to Download & Install Node.js - NPM on Windows

Step 3) Now, your computer works as a server! If anyone tries to access your computer on port 8080, they will get a "Hello World!" message in return!

Step 4) Start your internet browser, and type in the address: http://localhost:8080

OutPut

How to Download & Install Node.js - NPM on Windows

Summary

  • We have seen the installation of Node.js via the msi installation module which is available on the Node.js website. This installation installs the necessary modules which are required to run a Node.js application on the client.
  • Node.js can also be installed via a package manager. The package manager for windows is known as Chocolatey. By running some simple commands in the command prompt, the Chocolatey package manager automatically downloads the necessary files and then installs them on the client machine.
  • A simple Node.js application consists of creating a server which listens on a particular port. When a request comes to the server, the client automatically sends a 'Hello World' response to the client.

 

YOU MIGHT LIKE: