How to Install Node.js and npm: Step-by-Step Guide

Node.js is a powerful JavaScript runtime built on Chrome’s V8 engine that allows developers to build scalable network applications. Along with Node.js, npm (Node Package Manager) is installed by default, which helps manage libraries and dependencies in your project. This guide will walk you through the process of installing Node.js and npm on different operating systems: Windows, macOS, and Linux.

Installing Node.js and npm on Windows

Step 1: Download Node.js

  1. Open your browser and go to the official Node.js website.
  2. On the homepage, you will see two versions of Node.js:
    • LTS (Long Term Support): This version is recommended for most users.
    • Current: This version includes the latest features but might be less stable.
  3. Choose the appropriate version for your needs and download the installer (usually a .msi file).

Step 2: Run the Installer

  1. Once the download is complete, open the .msi file.
  2. Follow the instructions in the installer. Make sure to:
    • Accept the license agreement.
    • Choose the destination folder for installation (or leave it as default).
    • Keep the option to add Node.js to the PATH environment variable checked.
  3. Complete the installation process by clicking Next and then Finish.

Step 3: Verify the Installation

To confirm that Node.js and npm have been installed successfully:

  1. Open the Command Prompt (you can search for it in the Start menu).
  2. Run the following commands:
node -v

This command should return the installed version of Node.js.

npm -v

This command will return the version of npm that was installed along with Node.js.

Installing Node.js and npm on macOS

Step 1: Install Node.js Using Homebrew (Recommended)

  1. If you don’t have Homebrew installed, install it by running the following command in Terminal:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  1. Once Homebrew is installed, you can install Node.js by running:
brew install node

This will install both Node.js and npm.

Step 2: Verify the Installation

Once the installation is complete, you can verify that Node.js and npm are installed correctly:

  1. Open Terminal.
  2. Run the following commands:
node -v
npm -v

Both commands should return the installed versions of Node.js and npm.

Alternative Method: Install Node.js from the Official Website

  1. Go to the Node.js website and download the macOS installer (.pkg file).
  2. Run the downloaded .pkg file and follow the on-screen instructions to install Node.js.
  3. After installation, verify by running node -v and npm -v in Terminal.

Installing Node.js and npm on Linux (Ubuntu/Debian-Based Systems)

Step 1: Update the Package Index

Before installing Node.js, make sure your system is up-to-date:

sudo apt update

Step 2: Install Node.js

There are multiple ways to install Node.js on Linux. The recommended method is by using the NodeSource repository to get the latest version.

  1. To add the NodeSource PPA, run the following commands:
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
  1. Once the PPA is added, install Node.js and npm with this command:
sudo apt-get install -y nodejs

Step 3: Verify the Installation

To confirm Node.js and npm are installed:

node -v
npm -v

This will display the installed versions.

Using Node Version Manager (nvm) to Manage Node.js Versions

For more flexibility, you can use nvm (Node Version Manager), which allows you to easily install and switch between different versions of Node.js.

Step 1: Install nvm

To install nvm, run the following command in your Terminal (works on Linux and macOS):

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.4/install.sh | bash

Step 2: After the installation, restart your Terminal.

Step 3: Install Node.js with nvm

Once nvm is installed, you can use it to install the latest LTS version of Node.js:

nvm install --lts

You can also list all available versions and install specific ones:

nvm list-remote
nvm install <version>

Step 3: Verify Installation

After installing Node.js using nvm, verify the installation:

node -v
npm -v

Conclusion

Installing Node.js and npm on any operating system is a straightforward process, whether you’re using an installer from the Node.js website, a package manager like Homebrew, or Node Version Manager (nvm). With Node.js set up on your machine, you’re ready to start building scalable applications or experimenting with the vast ecosystem of npm packages.

Whether you’re on Windows, macOS, or Linux, this guide has covered the essential steps to get you up and running with Node.js and npm in no time.

YouTube Video:

How to Install and Uninstall Node.js (npm) on MacOS: Homebrew, Manual, and NVM Methods