Installing Multiple Versions of Node.js Using nvm

  • Satish 

When working with Node.js, you might encounter situations where you need to install multiple versions of the runtime.

For example, maybe you have the latest version of Node set up on your machine, yet the project you’re about to start working on requires an older version. Or maybe you’re upgrading an old Node project to a more modern version and it would be handy to be able to switch between the two while you make the transition.

Without a good tool, this would mean spending a lot of time and effort manually uninstalling and reinstalling Node versions and their global packages. Fortunately, there’s a better way!

Introducing nvm

nvm stands for Node Version Manager. As the name suggests, it helps you manage and switch between different Node versions with ease. It provides a command-line interface where you can install different versions with a single command, set a default, switch between them and much more.

OS Support

nvm supports both Linux and macOS, but that’s not to say that Windows users have to miss out. There’s a second project named nvm-windows that offers Windows users the option of easily managing Node environments. Despite the name, nvm-windows is not a clone of nvm, nor is it affiliated with it. However, the basic commands listed below (for installing, listing and switching between versions) should work for both nvm and nvm-windows.

Installation

Let’s first cover installation for Windows, macOS and Linux.

Windows

First, we need to do a little preparation:

  • uninstall any existing versions of Node.js
  • delete any existing Node.js installation directories (such as C:\Program Files\nodejs)
  • delete the existing npm install location (such as C:\Users\<user>\AppData\Roaming\npm)

After this, download and run the latest stable installer and you should be good to go!

macOS/Linux

Unlike Windows, removing previous Node and npm installations in macOS and Linux is optional. If this is something you want to do, there are plenty of good resources available online. For example, here’s how to remove Node on macOS and on Linux. And here’s how you can remove any previous npm installation you might have.

You can install nvm using cURL or Wget. On your terminal, run the following:

With cURL:

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

Or with Wget:

wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.35.2/install.sh | bash

Note that the version number (v0.35.2) will change as the project develops, so it’s worth checking the relevant section of project’s home page to find the most recent version.

This will clone the nvm repository to ~/.nvm and will make the required changes to your bash profile, so that nvm is available from anywhere in your terminal.

And that’s it! Reload (or restart) your terminal and nvm is ready to be used.

Using nvm

If installed correctly, the nvm command is available anywhere in you terminal. Let’s see how to use it to manage Node.js versions.

Install Multiple Versions of Node.js

One of the most important parts of nvm is, of course, installing different versions of Node.js. For this, nvm provides the nvm install command. You can install specific versions by running this command followed by the version you want. For example:

nvm install 12.14.1

By running the above in a terminal, nvm will install Node.js version 12.14.1.

Tip: nvm-windows users will have to run nvm use 12.14.1 after installing.

nvm follows SemVer, so if you want to install, for example, the latest 12.14 patch, you can do it by running:

nvm install 12.14

nvm will then install Node.js version 12.14.X, where X is the highest available version. At the time of writing, this is 1, so you’ll have the 12.14.1 version installed on your system.

You can see the full list of available versions by running:

nvm ls-remote

For nvm-windows, this is:

nvm ls available

Tip: Listing all available Node versions produces a lot of output. Linux users might like to qpipe that to less or grep the version they’re after. For example, nvm ls-remote | less, or nvm ls-remote | grep v12.

npm

When installing a Node.js instance, nvm will also install a compatible npm version. Each Node version might bring a different npm version, and you can run npm -v to check which one you’re currently using. Globally installed npm packages aren’t shared among different Node.js versions, as this could cause incompatibilities. Rather, they’re installed alongside the current Node version in ~/.nvm/versions/node/<version>/lib/node_modules. This has the added advantage that users won’t require sudo privileges to install global packages.

Fortunately, when installing a new Node.js version, you can reinstall the npm global packages from a specific version. For example:

nvm install v12.14.1 --reinstall-packages-from=10.18.1

By running the above, nvm will install Node.js version 12.14.1, the corresponding npm version, and reinstall the global npm packages you had installed for the 10.18.1 version.

If you’re not sure what the latest version is, you can use the node alias:
nvm install node

This will currently pull in version 13.6.0.

Or you can install the most recent LTS release, using:

nvm install --lts

This will currently pull in version 12.14.1.

You can also uninstall any instance you no longer think is useful, by running:

nvm uninstall 13.6.0

Switching Between Versions

So far, we’ve seen how to install different Node versions. Now let’s go through how to switch between them. Let me first note that when a new version is installed, it’s automatically put to use. So if you install the latest Node.js version, and run node -v right after, you’ll see the latest version output.

To switch through installed versions, nvm provides the nvm use command. This works similarly to the install command. So, you need to follow this by a version number or an alias.

Switch to Node.js version 13.6.0:

nvm use 13.6.0

Switch to Node.js version 12.14.1:

nvm use 12.14.1

Switch to the latest Node.js version:

nvm use node

Switch to the latest LTS version:

nvm use --lts

When switching to a different version, nvm will make the node instance in your terminal symlink to the proper Node.js instance.

Custom Aliases

You can also create custom aliases beyond the ones that come with nvm. For example, by running:

nvm alias awesome-version 13.6.0

You’re setting an alias with the name “awesome-version” for Node.js version 13.6.0. So, if you now run:

nvm use awesome-version
nvm will switch node to version 13.6.0. You can delete an alias by running:
nvm unalias awesome-version

You can also set a default instance to be used in any shell, by targeting a version to the “default” alias, like so:

nvm alias default 12.14.1

Listing Installed Instances

At any time you can check which versions you have installed by running:

nvm ls

This will display something resembling the following:

nvm versions list

The entry in green, with an arrow on the left, is the current version in use. Below the installed versions, there’s a list of available aliases. Try executing the following now:

nvm use node
nvm ls

It will display like so:

nvm use and versions list

You can also check what is the current version in use with the command:

nvm current

Specify a Node Version on a Per-project Basis

Version managers such as rbenv allow you to specify a Ruby version on a per-project basis (by writing that version to a .ruby-version file in your current directory). This is kind of possible with nvm in that, if you create a .nvmrc file inside a project and specify a version number, you can cd into the project directory and type nvm use. nvm will then read the contents of the .nvmrc file and use whatever version of Node you specify.

If it’s important to you that this happens automatically, there are a couple of snippets on the project’s home page for you to add to your .bashrc or .zshrc files to make this happen.

Here’s the ZSH snippet. Place this below your nvm config:

autoload -U add-zsh-hook
load-nvmrc() {
  local node_version="$(nvm version)"
  local nvmrc_path="$(nvm_find_nvmrc)"

  if [ -n "$nvmrc_path" ]; then
    local nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")")

    if [ "$nvmrc_node_version" = "N/A" ]; then
      nvm install
    elif [ "$nvmrc_node_version" != "$node_version" ]; then
      nvm use
    fi
  elif [ "$node_version" != "$(nvm version default)" ]; then
    echo "Reverting to nvm default version"
    nvm use default
  fi
}
add-zsh-hook chpwd load-nvmrc
load-nvmrc

Now, when you change into a directory with a .nvmrc file, your shell will automatically change Node version.

Automatically apply nvm use

Other nvm Commands

nvm provides a couple of other commands that are more advanced or less commonly used.

You can run a command directly for an installed version without switching the node variable:

nvm run 13.6.0 --version

You can run a command on a sub-shell, targeting a specific version:

nvm exec 13.6.0 node --version

You can get the path to the Node.js executable of a specific installed version:

nvm which 13.6.0

This might be useful when configuring a text editor plugin that needs to know where your current version of Node lives.

Conclusion

nvm is a great tool for any Node.js developer. It enables a concern-free installation and easy switching between different versions, saving time for what really matters.

A thank you note to Tim Caswell, the creator of nvm, and also to Corey Butler for the nvm for Windows support, and of course to those contributing to these great projects. Your work is greatly appreciated by the Node.js community.