How to Install Node.js on Windows, macOS, and Linux

Node.js is a popular JavaScript runtime that allows developers to build scalable web applications, backend services, and more. In this guide, we will walk you through the steps to install Node.js on Windows, macOS, and Linux.

1. Check if Node.js is Already Installed

Before installing, check if you already have Node.js by running the following command in your terminal or command prompt:

node -v

If Node.js is installed, this command will return the installed version number. If not, proceed with the installation.

2. Installing Node.js on Windows

Using the Official Installer

  1. Visit the official Node.js website: https://nodejs.org

  2. Download the LTS (Long-Term Support) version for stability.

  3. Open the downloaded .msi installer and follow the setup instructions.

  4. Ensure you check the box for adding Node.js to PATH during installation.

  5. Once installed, verify by running:

node -v
npm -v

3. Installing Node.js on macOS

Using Homebrew (Recommended)

Homebrew is a package manager for macOS that makes installations easier.

  1. Open Terminal and install Homebrew (if not installed):

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Install Node.js:

brew install node

Verify installation:

node -v
npm -v

4. Installing Node.js on Linux

Using Node Version Manager (NVM) - Recommended

NVM allows you to install and manage multiple Node.js versions easily.

  1. Install NVM:

curl -fsSL https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

Reload shell:

source ~/.bashrc  # or source ~/.zshrc if using zsh

Install the latest LTS version of Node.js:

nvm install --lts

Verify installation:

node -v
npm -v

Using Package Managers

For Ubuntu/Debian:

sudo apt update && sudo apt install nodejs npm -y

For Fedora:

sudo dnf install nodejs

5. Verifying Node.js and npm Installation

Run the following commands to ensure Node.js and npm (Node Package Manager) are installed correctly:

node -v  # Check Node.js version
npm -v   # Check npm version

6. Managing Node.js Versions

If you need to switch between multiple Node.js versions, use NVM:

nvm list  # List installed versions
nvm use <version>  # Switch to a specific version

Conclusion

Congratulations! You have successfully installed Node.js on your system. You can now start building JavaScript applications and using npm to manage dependencies.

For more tutorials, visit CoderCrafter.