How to Manage Node.js Versions with NVM

Managing multiple Node.js versions on a single machine is essential when different projects require different Node environments. This article provides a quick, practical guide on how to install Node Version Manager (NVM), install different Node.js versions, switch between them, and set a default version for your system.

Installing NVM

To get started, you need to install NVM on your machine.

For macOS and Linux, run the installation script using cURL or Wget in your terminal:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash

After running the script, restart your terminal or run source ~/.bashrc (or source ~/.zshrc depending on your shell) to reload the profile.

For Windows, NVM is not natively supported, but you can download and install the popular alternative nvm-windows installer.

To verify that NVM is installed correctly, run:

nvm --version

Installing Node.js Versions

Once NVM is installed, you can download and install any version of Node.js.

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

nvm install --lts

To install the absolute newest release:

nvm install node

To install a specific version (for example, version 18.16.0):

nvm install 18.16.0

Listing Your Installed Node.js Versions

To see all the versions of Node.js currently installed on your local machine, use:

nvm ls

To view a list of all available Node.js versions that you can download from the official registry, use:

nvm ls-remote

Switching Between Node.js Versions

Switching to a different installed version of Node.js is instantaneous. Run the use command followed by the version number:

nvm use 18.16.0

If you only specify the major version, NVM will automatically switch to the latest installed minor/patch version of that release:

nvm use 18

To verify the version currently active in your terminal, run:

node -v

Setting a Default Node.js Version

When you open a new terminal window, NVM resets to a default version. You can set a specific version as your system default using the alias command:

nvm alias default 18.16.0

Now, every new terminal session will automatically start with the designated default Node.js version.