📒 

How to install Node.js™

Node.js™ is a powerful and popular open-source runtime environment that allows you to run JavaScript code outside of a browser, typically on servers. It is known for its non-blocking, event-driven architecture, making it a preferred choice for building fast, scalable applications. Whether you’re a developer looking to dive into backend development or planning to build real-time applications like chat apps or online collaboration tools, installing Node.js on your system is the first step.

This guide will walk you through everything you need to know about installing Node.js, covering multiple operating systems and ways to keep Node.js up-to-date.

Step 1: Choose an Installation Method

There are several ways to install Node.js, depending on your operating system and personal preferences. You can install Node.js via the official installer, a package manager, or by using a version manager like nvm (Node Version Manager). Let’s look at each method in detail.

Step 2: Install Node.js on Windows

Option 1: Using the Node.js Installer

The easiest way to install Node.js on Windows is to download the installer from the official Node.js website.

  1. Go to the Node.js website: Visit nodejs.org.
  2. Download the Installer: On the homepage, you’ll see two versions:
    • LTS (Long Term Support): This version is more stable and is recommended for most users.
    • Current: This version contains the latest features and updates, but may not be as stable.

    Choose the version based on your project’s needs and click the link to download the appropriate installer.

  3. Run the Installer: Once the download is complete, run the installer. Follow the prompts in the setup wizard, which will guide you through the process. By default, it installs Node.js in the appropriate directories and adds it to your system’s PATH.
  4. Verify the Installation: Open the command prompt and type:
    node -v

    This command will display the version of Node.js installed. Similarly, check for the version of npm (Node Package Manager) with:

    npm -v

    Both Node.js and npm should be installed successfully.

Option 2: Using Chocolatey (Windows Package Manager)

If you prefer to use a package manager, you can install Node.js via Chocolatey. First, ensure Chocolatey is installed on your system. Then, open an Administrator Command Prompt and run the following command:

choco install nodejs-lts

This will install the LTS version of Node.js.

Step 3: Install Node.js on macOS

Option 1: Using the Node.js Installer

  1. Download the Installer: Visit nodejs.org and download the macOS installer, similar to the Windows steps.
  2. Run the Installer: Open the downloaded .pkg file and follow the installation wizard to complete the installation.
  3. Verify Installation: Open Terminal and type:
    node -v
    npm -v

Option 2: Using Homebrew

Homebrew is a popular package manager for macOS. If you already have Homebrew installed, you can use it to install Node.js.

  1. Open Terminal.
  2. Run the following command to install Node.js:
    brew install node
  3. Verify Installation: Once the installation is complete, check the versions of Node.js and npm by running:
    node -v
    npm -v

Step 4: Install Node.js on Linux

Option 1: Using the Node.js Binary Distributions

Node.js offers precompiled binaries for Linux distributions like Ubuntu, Debian, and Fedora. You can add the Node.js repository and install the latest version using your system’s package manager.

  1. Add Node.js Repository: Open Terminal and run the following commands for Ubuntu/Debian:
    curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -

    This command adds the Node.js LTS repository to your system.

  2. Install Node.js:
    sudo apt-get install -y nodejs
  3. Verify Installation:
    node -v
    npm -v

For Fedora, CentOS, and other RPM-based distributions, use:

curl -fsSL https://rpm.nodesource.com/setup_lts.x | sudo bash -
sudo yum install -y nodejs

Option 2: Using Node Version Manager (nvm)

nvm is a popular tool for managing multiple Node.js versions on the same system, which is especially helpful if you work on multiple projects with different version requirements.

  1. Install nvm: Run the following command to download and install nvm:
    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.2/install.sh | bash
  2. Install Node.js: Once nvm is installed, you can install the latest LTS version of Node.js by running:
    nvm install --lts
  3. Set Default Node Version: You can set a default Node.js version to use by running:
    nvm use --lts
  4. Verify Installation:
    node -v
    npm -v

Step 5: Keeping Node.js Up-to-Date

To ensure that your Node.js environment stays up-to-date with the latest security patches and features, regularly check for updates:

  • Using nvm: Run the following command to update Node.js to the latest version:
    nvm install node --reinstall-packages-from=node
  • Using a Package Manager (Linux/macOS): Update Node.js by running:
    sudo apt-get update && sudo apt-get upgrade nodejs
  • Using the Installer (Windows/macOS): Simply download and run the latest version of the Node.js installer from nodejs.org and follow the prompts.

Step 6: Install Global npm Packages

Once Node.js is installed, you can install global npm packages. Global packages are used for command-line tools that can be accessed from anywhere on your system. For example, to install the popular package manager yarn, run:

npm install -g yarn

You can list installed global npm packages using:

npm list -g --depth=0

Conclusion

Installing Node.js is a relatively straightforward process across different operating systems, whether you prefer using an installer or package manager. After installation, you’ll have access to a robust environment to run JavaScript applications and utilize npm to manage dependencies. Whether you’re a beginner or an experienced developer, knowing how to install and manage Node.js will help you get the most out of this powerful platform for building scalable, high-performance applications.