Installing Node.js on macOS

The best way to install Node.js on macOS is by using nvm (Node Version Manager), a utility that lets you install and manage different Node.js versions on your macOS. This article will guide you through the process.

1. Make sure you have Homebrew installed

Before you start, make sure you have Homebrew, the most popular macOS package manager.

If you don't have it installed on your machine, you can install it by running the following command in your terminal:

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

2. Install nvm

To install nvm using Homebrew, run the following command:

brew install nvm

3. Configure your shell profile file for nvm

Once nvm is installed, you need to add its configuration to your shell's startup file. This ensures nvm is loaded whenever you open a new terminal session.
For that, add the following lines to your shell's startup file which is ~/.zshrc for Zsh, ~/.bashrc for Bash, and ~/.config/fish/config.fish for Fish:

export NVM_DIR="$HOME/.nvm"
  [ -s "/opt/homebrew/opt/nvm/nvm.sh" ] && \. "/opt/homebrew/opt/nvm/nvm.sh"  # This loads nvm
  [ -s "/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm" ] && \. "/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm"  # This loads nvm bash_completion

Next, open a new terminal for the changes to take effect and run:

nvm -v

This command should display the installed nvm version.

4. Install the latest LTS version of Node.js

To install the latest Long-Term Support (LTS) version of Node.js, run:

nvm install --lts

5. Verify the installation

Open a new terminal window and run the following commands to confirm successful installation:

node -v
npm -v
npx -v

You should see the installed versions of Node.js, npm and npx displayed.