Recently, I had to install Node.js and npm on a Mac and I decided to document the steps I took, which could be useful if we have to do this again from scratch.

Choosing the Node.js version manager

As we can read in the installation and setup documentation for npm (Using a Node Version Manager):

There are a lot of different versions of Node out there. These tools will help you keep track of what version you are using, and also make it easy to install new versions and switch between them. They also make npm easier to set up :)

I decided to go with NVM (Node Version Manager), which seems to be widely adopted and it’s also the one I was familiar with.

nvm installation

First of all we need to install nvm in our system by following the instructions on its GitHub README page.

I usually prefer to go with a manual installation in order to be more in control of how things are set up. The Git Install option fits this approach:

  • Clone the repository and checkout the latest version:
$ cd ~/
$ git clone https://github.com/nvm-sh/nvm.git .nvm
$ cd ~/.nvm
$ git checkout v0.39.7
  • Activate nvm by sourcing it from the shell:
$ . ./nvm.sh
  • Add these lines to the ~/.zshrc file to have it automatically sourced in interactive shells:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

NOTE: it assumes we’re using zsh.

  • Verify installation:
$ nvm --version
0.39.7

node / npm installation

Check that we don’t currently have any versions of Node.js installed:

$ nvm current
none

Now we can install different versions of Node.js / npm with nvm:

$ nvm install 16.13.0
Downloading and installing node v16.13.0...
Downloading https://nodejs.org/dist/v16.13.0/node-v16.13.0-darwin-x64.tar.xz...
################################################################################################################################ 100.0%
Computing checksum with shasum -a 256
Checksums matched!
Now using node v16.13.0 (npm v8.1.0)
Creating default alias: default -> 16.13.0 (-> v16.13.0)

Verify versions

And then we can verify the versions of node and npm that are currently active:

$ nvm current
v16.13.0

$ node --version
v16.13.0

$ npm --version
8.1.0

npm version

When installing Node.js, each version of node is bundled with a version of npm:

nvm usage

List node versions

We can list all the Node.js versions we’ve got installed:

$ nvm ls
->     v16.13.0
default -> 16.13.0 (-> v16.13.0)
node -> stable (-> v16.13.0) (default)
stable -> 16.13 (-> v16.13.0) (default)

And all the available versions out there:

$ nvm ls-remote

Switch between node versions

If we had multiple versions of Node.js installed then we could switch between them:

$ nvm use 16.13.0
Now using node v16.13.0 (npm v8.1.0)

.nvmrc

As explained in the nvm documentation, we can create a .nvmrc file containing a Node.js version number in the project directory (or any parent directory). Afterwards, nvm commands will automatically use the version specified in the .nvmrc  file if no version of Node.js is supplied.

$ cd /full/path/to/my_project
$ cat .nvmrc
17.8.0
$ nvm use
Found '/full/path/to/my_project/.nvmrc' with version <17.8.0>
Now using node v17.8.0 (npm v8.5.5)

Upgrading nvm

Since we’ve just cloned the nvm repository, we can just fetch and check out the latest release in order to upgrade it. See the Manual Upgrade notes.