Install Node JS

Video Tutorial
FREE
Introduction to Node.js thumbnail
This video belongs to
JavaScript Course With Certification: Unlocking the Power of JavaScript
9 modules
Certificate
Topics Covered

Overview

Node.js has become quite popular since its first release in 2009. Its usage on 30 million sites and about 1.5 billion downloads show that it stands firm in the market, with startups choosing it over other options. The speed and performance of Node.js have given excellent results to big companies like Netflix, NASA, PayPal, LinkedIn, and many others. Node.js installation can be done in various ways with the help of different package managers. Node.js offers asynchronous and event-driven architecture, efficiency, reusability, and scalability.

Setting up the Node Development Environment

As you must have read what Node.js is for in the previous section, let’s see how to set up and test the Node development environment on various operating systems. The first step in setting up the Node Development Environment is to install Node.js.

Install Node.js

Nodejs official website

The way to install Node.js depends on what operating system you are using.

Operating SystemDirections
Mac OSWe can install Node.js on MacOS easily by using the installer from the official Node.js website. The alternative way to install Node.js is to use Homebrew and NVM (Node Version Manager). NVM also helps in the easy management of multiple active Node.js versions.
WindowsWe can install Node.js on Windows easily by using the installer from the official Node.js website. The alternative way to install Node.js is to use Chocolatey and nvm-windows(Node Version Manager). nvm-windows also helps in the easy management of multiple active Node.js versions
Linux (Ubuntu)There are various package managers available for each distribution. We can install Node.js by using the Ubuntu official repository. Alternatively, we can install Node.js in Linux using Node Source and personal package archive (PPA), it is recommended to use nvm to manage multiple active Node.js versions.

Install a Text Editor

You can use any text editor of your choice. Here is a list of some recommended ones:

  1. Microsoft Visual Studio Code - One of the most popular IDE. It is free and comes with a lot of helpful tools pre-installed and useful extensions.
  2. Cloud 9 - A free cloud-based IDE with a lot of features.
  3. IntelliJ IDEA - Developed by JetBrains. It can be used to develop Node.js applications with the help of the Node.js plugin.
  4. Sublime Text - It is a lightweight IDE with features like split editing, customizing, project switching, etc.
  5. Vim - It is a light and easy text editor. It is free to use.

Starting a New Project

To start a new project we use the command npm init. On execution, it asks a set of questions regarding the application. On completion, it creates a package.json file.

We can now start working on our project.

How to Install Node.js on Windows

There are two methods to install Node.js on the Windows system environment. Every operating system has a different installation method of Node.js, depending on the existing environment of the system.

  • By using the installer from the Node.js website.
  • By using the Chocolatey package manager of Windows.

Using the Installer from the Node.js Official Website to Install Node.js on Windows

It is the easiest way of installing Node.js. Visit the official Node.js website. There are two kinds of builds available on the website: LTS(long-term support) which is recommended and Current which contains the latest features. The LTS releases are highly refined and bug-free. The Current builds contain the new features, but they may not be complete. Hence it is recommended to use LTS builds. The steps to install Node.js on Windows are as follows:

  1. Download the Windows installer (.msi) file according to your system environment (64-bit or 32-bit). It contains a collection of essential files to install, update, or modify the Node.js versions on your system.

  2. Run the Node.js installer file. Double-click the (.msi) installer file. Click on the “Run” button on the first screen to begin the installation.

  3. Continue the installation steps. On the next screen, click the “Next” button to continue with the installation.

  4. Accept terms and conditions. Click on the “Next” button.

  5. Setup the directory path where you want Node.js files to be installed in your system. Click on the “Next” button to continue.

  6. Select the default components to be installed. Click the “Next” button.

  7. Start the installation and wait till it is finished. Click the “Finish” button to complete the installation.

  8. Verify the Node.js installation. Open the command prompt and type the following commands

    The output will be the version of the node installed. Similarly for npm do,

    The output will be the version of npm installed.

Note: You do not need to set up the environment variables path, the installer will take care of the environment variables. Also (.msi) installer includes the NPM (Node Package Manager). Hence no need to download it separately.

Using Chocolatey Package Manager to Install Node.js on Windows

  1. First, install Chocolatey (if already installed, move to the 3rd step).

  2. Run Command Prompt as administrator. Run the following command to install Chocolatey.

  3. Install Node.js to your local machine using the Chocolatey package manager.

  4. Verify the Node.js installation. Open the command prompt and type the following commands

    The output will be the version of the node installed. Similarly for npm do,

    The output will be the version of npm installed.

How To Install Node.js on macOS

There are several ways to install Node.js on MacOS:

  • By using the installer from the Node.js official website.
  • By using the Homebrew package manager of macOS.
  • By using Node Version Manager (nvm).

Using the Installer from the Node.js Official Website to Install Node.js on macOS

It is the easiest way of installing Node.js. Visit the official Node.js website and download the pre-built .pkg installer. There are two kinds of builds available on the website: LTS(long-term support) which is recommended and Current which contains the latest features. The LTS releases are highly refined and bug-free. The Current builds contain the new features, but they may not be complete. Hence it is recommended to use LTS builds. The steps to install Node.js on MacOS are as follows:

  1. Download the .pkg installer: Select Open with Installer and run it.

  2. Run the installer.

  3. Introduction window, On this screen click continue.

  4. Licence window, click agree and then press the continue button.

  5. Installation Type, click install and authenticate using your macOS username and password the continue installation.

  6. After successful completion a summary will be shown saying that Node.js and npm were installed.

  7. Close the installation wizard.

  8. Verify the Node.js installation. Open the command prompt and type the following commands

    The output will be the version of the node installed. Similarly for npm do,

    The output will be the version of npm installed.

Using Homebrew to Install Node.js on macOS

Homebrew is one of the most popular package managers of macOS and it makes installing Node.js relatively easy.

  1. Check if you have Homebrew installed, in your terminal type:

  2. If Homebrew is installed it will give the installed version. If Homebrew is not installed you can install it with the following command:

  3. After installation of Homebrew, install Node.js

  4. After this you can verify the installation by node -v and npm -v.

Note: Homebrew always installs the latest version of Node.js.

Using Node Version Manager (nvm) to install Node.js on macOS

NVM( Node Version Manager) is a bash script that helps in efficiently managing multiple active Node.js versions. To steps to install NVM and then Node.js using NVM are:

  1. To clone the nvm repository and add the source lines to shell profile(~/.bash_profile, ~/.zshrc, ~/.profile, or ~/.bashrc):

    • For curl
    • For gwet
  2. Reload the shell configuration by using the source command. For example :

  3. Verify installation.

    The above command will print nvm if installation was successful.

  4. Now install Node.js with the help of node version manager (nvm).

    • For the latest release of Node
    • For a specific version of Node.js
    • Now to use that particular version of Node.js
    • To delete a version of Node.js
    • To list all local versions of Node.js available on the system
  5. To install npm (npde package manager)

How To Install Node.js on Ubuntu (Linux):

There are several ways to install Node.js on Ubuntu (Linux):

  • By using apt with Ubuntu’s official repositories.
  • By using the Node Source and personal package archive (PPA).
  • By using Node Version Manager (NVM).

Using Ubuntu's Official Repository to Install Node.js on Linux

It is the best option to set up Node.js and npm on Ubuntu and is sufficient for many developers. Ubuntu 20.04 contains a Node.js version 10.19 in its default repository that can be used across multiple systems. Follow the following steps,

  1. Refresh your local package index using
  2. Install Node.js using the following command
  3. Now install npm
  4. Verify the Node.js installation

Using Node Source and PPA (personal package archive) to Install Node.js on Ubuntu

You can install different versions of Nodejs with the help of PPA (personal package archive) maintained by NodeSource. These PPA have many versions of Nodejs available like v14 and v16. Follow these steps for successful installation:

  1. First, we need to install PPA to get access to its packages.

    Replace 16.x with your preferred version of Node.js in the above script This script will add the NodeSource signing key to your system, create an apt repository and install the necessary packages.

  2. Now run the script.

    This will add PPA configuration, and your local cache will be updated automatically.

  3. You can now install Node.js and npm.

  4. Verify the Node.js installation.

Note: The NodeSource package contains both the node binary and npm, so you don't need to install them separately.

Using Node Version Manager (nvm) to Install Node.js on Ubuntu

It is another way of installing Node.js and npm on Ubuntu. Follow these steps for Node.js installation:

  1. First you need to install nvm using the following curl command.

    This will install the nvm script to the user account.

  2. Now to use it, you must first source your .bachrc file.

  3. Now you can ask nvm, which version of Node.js are available.

  4. Now you can install any of the above versions or versions.

  5. Now you can check different versions of Node.js installed using.

    This will show the currently active version on the first line, followed by some named aliases.

    • You can switch the Node.js version using

Updating the Local npm Version of Windows, macOS, and Linux (Ubuntu)

NPM is one of the most prominent package managers for Node.js. It manages the dependencies of packages. NPM takes care of automatically unpacking all the packages every time we use them in a project. Regularly updating npm improves the code used in the projects. The process of checking and updating the npm version in Windows, macOS, and Linux is discussed below:

Updating local npm version on Windows:

  1. Open the command prompt and check the version of npm.
  2. To update, run this command
    After running this command, the npm version will be updated. You can recheck the npm version by using the step1 command.

Updating local npm version on macOS:

  1. Open the terminal and check the version of npm.
  2. To update, run this command
    After running this command, the npm version will be updated. You can recheck the npm version by using the step1 command.

Updating local npm version on Ubuntu:

  1. Run the following commands one by one to update the npm

How does Node.js Work?

Nodejs internal working

Node.js is a Javascript run time environment built on top of Chrome's V8 engine. It is single-threaded, although it provides asynchronous operations with the help of the Libuv library.

Let's understand the components of Node.js: V8 engine: It is an open-source project by Google. The primary purpose of the V8 engine is to run the javascript code outside the browser and convert javascript code directly to machine-level code for faster execution. It gives access to the Node, underlying networking and helps the Node to handle aspects of concurrency.

Libuv: It is responsible for all the asynchronous operations. It performs, maintains, and manages all the I/O operations and events in the event pool. All the TCP level connectivity and file/system operations are performed by this library. This library is written in C. Libuv implements an extremely important feature of Node.js which is Event Loop.

Event Loop: The event loop in Node.js helps you in performing non-blocking I/O operations even though JavaScript is single-threaded. The event loop is a limitless loop that assigns operations to the operating system whenever and wherever possible. The event loop is initialized as soon as Node.js starts, thus when a multi-threaded operating system finishes one of its many tasks, the kernel tells Node.js, and accordingly, the assigned callbacks to that task are added to the event queue, which is executed as soon as the primary call stack is empty.

Working of event loop in brief:

  • Asynchronous functions emit events like promises used for fetching data from the database etc.
  • After that, Event-loop picks up those events from the event queue and processes them when the main stack is empty.
  • Callback functions are executed after that.

Example:

Output:
When you execute the above code, you will see the following output:

Thus Node.js holds the two libraries (V8 and Libuv) together; thereby, it becomes a unique interface and solution for developers. Hence can write your whole code in Javascript and compile it efficiently without interacting with C++ code directly.

Advantages of Using Node.js

There are various advantages of Node.js. Some of the following are

  1. Simplicity - Javascript is one of the most liked and widely used languages in the programming community. It has quite a simple syntax; hence one can quickly adapt it. It also reduces the time to write codes. Node.js has an easy learning curve as anyone who knows the concepts of server-side programming (Javascript) can learn it in no time. Also developers can use a single language to develop entire full-stack applications (frontend and backend).

  2. Scalability - Node.js provides a lot of features like cluster modules, non-blocking event loop mechanisms, and microservices. All of these make a Node.js application highly scalable.

  3. Open Source - Node.js has the support of a large community of developers that have created numerous open-source modules for anyone to use.

  4. Cross-platform Development - With Node.js, one does not need to spend time writing separate code for Windows, Linux, and Mac. We can use the same code for a mobile application and a desktop version.

  5. Cost-Effective - Node.js allows users to write both server and client-side codes in Javascript. Hence there is no need of hiring a separate group of developers for each of them.

  6. Real-Time Applications - Node.js offers high performance in Real Time applications due to its ability to multitask. The speed of I/O operations is enhanced by the non-blocking event loop.

  7. Large Community - Node.js has a large community of developers that are constantly working to improve its performance.

  8. Caching - Node.js provides caching of single modules. Requests get cached in application memory. This provides faster loading.

Conclusion

  • Node.js is an open-source, cross-platform Javascript runtime environment that runs on Chrome's V8 engine internally.
  • For setting up the Node.js development environment: install Node.js, install a text editor, and initialize the project with the command npm init.
  • Node.js can be installed on Windows by the installer provided on the node.js website or by using the package manager Chocolatey.
  • Node.js can be installed on macOS by the installer provided on the node.js website or by using the package manager Homebrew.
  • NVM, Node Version Manager, can be used to manage multiple active node versions easily.
  • Node.js can be installed on Ubuntu by using Ubuntu's official repositories, by using the Node Source and personal package archive (PPA), and by using Node Version Manager (NVM).
  • To update npm on Windows and macOS we use the command
  • To update npm on Linux we use the following commands:
  • NodeJs internally works with the help of Chrome’s V8 engine and Libuv library. These help Nodejs to become non-blocking, concurrent, event-driven, and robust.