Summary: in this tutorial, you will learn how to install and use Node Version Manager (NVM) on macOS.
Node Version Manager (NVM) allows you to install multiple versions of Node.js on your macOS and switch between them easily.
This can be helpful when you have different Node.js projects that require different versions of Node.js.
Install NVM
We’ll show you how to install NVM using Homebrew.
Step 1. Install Homebrew
If Homebrew is not available, you can install it by running the following command:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"Code language: JavaScript (javascript)Step 2. Install NVM
After installing Homebrew, you can install nvm by running the following command:
brew install nvmStep 3. Add nvm to your shell profile.
To enable nvm every time you open a new terminal, you need to add the following line to your shell configuration files (~/.zshrc, ~/.bashrc, or ~/.bash_profile):
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
Code language: PHP (php)Save the file and reload your shell configuration:
source ~/.zshrcInstall Node.js via nvm
Run the nvm install command to install a specific Node.js version on your macOS:
npm install <version>Code language: HTML, XML (xml)Replace the <version> with the version number you want to install, for example:
nvm install 20.6.0Code language: CSS (css)To install the latest Long-term support (LTS) version, you can use the following command:
nvm install --ltsIf you want to install the latest Node.js, you can use the following command:
nvm instal nodeList installed Node.js versions
Run the nvm list command to list out all the installed Node.js versions:
nvm listCode language: PHP (php)The output looks like the following:
v16.20.1
v18.17.0
-> v20.16.0
default -> v16.20.1
node -> stable (-> v20.16.0) (default)
stable -> 20.16.0 (-> v20.16.0)
iojs -> N/A (default)
lts/* -> lts/hydrogen (-> v18.17.0)
lts/hydrogen -> v18.17.0Code language: JavaScript (javascript)-> v20.16.0: The arrow indicates the currently active version of Node.js.default -> v16.20.1: The default version thatnvmwill use when you open a new terminal session.node -> stable: Indicates thatnodepoints to the stable version, which in this case isv20.16.0.lts/*: Shows the versions associated with the Long-Term Support (LTS) releases. For example,lts/hydrogenpoints tov18.17.0.
Switch Node.js versions
To switch to a specific Node.js, you use the following command:
nvm use <version>Code language: HTML, XML (xml)Replace <version> with a version number you want to switch to. For example:
nvm use 20.16.0Code language: CSS (css)And you can verify the currently active Node.js version:
node -vOutput:
v20.16.0Code language: CSS (css)Uninstall a Node.js version
To uninstall a specific Node.js version on your computer:
First, list out all the installed versions using the nvm list command:
nvm listCode language: PHP (php)Second, run the uninstall command:
nvm uninstall <version>Code language: HTML, XML (xml)Replace the <version> with the one you want to uninstall.
Summary
- Use Node Version Manager (NVM) to manage and switch between multiple versions of Node.js on your macOS.
- Use
nvm installcommand to install a specific Node.js version. - Use
nvm listcommand to display a list of installed Node.js versions on your computer. - Use
nvm usecommand to switch to a specific Node.js version. - Use
node -vcommand to show the currently active Node.js version. - Use
nvm uninstallcommand to uninstall a specific Node.js version on your computer.