When developing Node.js applications, you might face situations where you need to install multiple versions of Node. This can happen when you have multiple projects and they have different requirements, or you have a deployable application which must be compatible with different Node versions. Without a good tool, this would mean a lot of work and effort to install the different versions manually, and basing a project on a specific version. 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 which offers Windows users the possibility 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, make sure you uninstall any Node.js version you might have on your system, as they can collide with the installation. After this, download the latest stable installer. Run the executable installer, follow the steps provided and you're good to go!
macOS/Linux
Removing previous Node installations is optional, although it’s advised you do so. There are plenty of good online resources for how you might do this (macOS, Linux). It’s also good if you remove any npm installation you might have, since it might collide with nvm's installation. You'll also need to have a C++ compiler installed on your system. For macOS, you can install the Xcode command line tools. You can do this by running the following command:
xcode-select --install
On Linux, you can install the build-essential package by running the following (assumes apt):
sudo apt-get update
sudo apt-get install build-essential
Having the required C++ compiler, you can then install nvm using cURL or Wget. On your terminal, run the following:
With cURL:
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash
Or with Wget:
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash
Note that the version number (v0.33.8) 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.
That's it, nvm is installed and 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 8.9.4
By running the above in a terminal, nvm will install Node.js version 8.9.4. nvm follows SemVer, so if you want to install, for example, the latest 8.9 patch, you can do it by running:
nvm install 8.9
nvm, will then install Node.js version 8.9.X, where X is the highest available version. At the time of writing, this is 4, so you'll have the 8.9.4 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
Continue reading %Installing Multiple Versions of Node.js Using nvm%