Getting Started with Node Version Manager (NVM) on Mac

First make sure you have Homebrew installed. If not then follow the instructions on the Homebrew website.

If it is installed then run:

brew update
Update brew

Once this has finished then run:

brew install nvm
Install NVM using Homebrew

Once this has completed there were some caveats in the outlined in the terminal.

You should create NVM's working directory if it doesn't exist:
  mkdir ~/.nvm

Add the following to your shell profile e.g. ~/.profile or ~/.zshrc:
  export NVM_DIR="$HOME/.nvm"
  [ -s "/usr/local/opt/nvm/" ] && \. "/usr/local/opt/nvm/"  # This loads nvm
  [ -s "/usr/local/opt/nvm/etc/bash_completion.d/nvm" ] && \. "/usr/local/opt/nvm/etc/bash_completion.d/nvm"  # This loads nvm bash_completion

NVM does work for the current session but if you restart the machine or start a terminal in another program such as VS Code then it won't work. In order to avoid this I needed to add following to the .profile and .zshrc files in the user (~) directory.

export NVM_DIR="$HOME/.nvm"
  [ -s "/opt/homebrew/opt/nvm/" ] && \. "/opt/homebrew/opt/nvm/"  # 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
Updating home directory

To check the version of NVM installed run:

nvm -v
Get version of NVM installed on machine

To check the version of node installed use:

node -v
Get version of Node installed on machine

To install the latest version of Node.js run:

nvm install --lts
Install latest version of Node.js

When the install is finished then version of Node what was installed becomes the active version.

To install a specific version of Node use:

nvm install 18 # install the most recent version of Node.js 18
nvm install 18.18.0 # install a more specific version of Node

To change the active version of Node use:

node -v
~ % v20.8.0
nvm use 18 # to change to Node.js version to 18.
~ % Now using node v18.8.0 (npm v9.8.1)
node -v
~ % v18.8.0

You can now easily switch between Node versions depending on your needs. This also updates the version of NPM being used.

Subscribe to Tom Conneely

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.