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 are about to start working on requires an older version.or may be you are 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 too much of time manually uninstalling and reinstalling Node versions and their global packages. Fortunately, there is a better way.
Introducing NVM
NVM stands for Node Version Manager. It is a great tool that helps you to 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 as default, switch between them, and much more.
OS Support
Node version manager supports both macOS and Linux, but that is not to say that Windows users have to miss out. But there is a second project named nvm-windows that offers Windows users the option for easily managing Node environments.
The basic commands listed below (for installing, listing, and switching between versions) should work for both nvm and nvm-windows.
Requirements
As it is likely that you have Node.js installed as the default installer of the official website, you need to uninstall it. Else you will have conflicts with the installed versions later.
After uninstalling, you then need to remove every possible installation directory that may remain on the disk (C:\Program Files\nodejs). upon verifying that the mentioned directory does not exist anymore, install any possible npm install locations too.
1. %USERPROFILE%/AppData\Roaming\npm (C:\Users\
2. %USERPROFILE%/AppData\Roaming\npm-cache (C:\Users\
After deleting the mentioned directories and the content, you may then proceed with the NVM installation for Windows.
Installation
How to do ‘Node.js install’ for Linux, macOS, and Windows.
Windows:
1. Uninstall any existing versions of Node.js
2. Delete any existing Node.js installation directories, such as C:\Program Files\nodejs
3. Delete the existing npm install location, such as C:\Users\
After this, download and run the latest stable installer and you are all set to go!
macOS/Linux:
Unlike Windows, removing previous Node and npm installations in macOS and Linux is optional. However, if you want to remove them, there are plenty of good resources available online.
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
With Wget –
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.35.2/install.sh | bash
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.
Then, reload or restart your terminal and nvm is ready to be used.
Ensure that nvm was installed correctly by running with nvm --version
command which would return the version od nvm installed.
Using nvm
If installed correctly, the nvm command is available anywhere in your 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 to install 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.
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. You can see the full list of available versions by running:
nvm ls-remote
For nvm-windows, this is:
nvm ls available
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 are presently using. Globally installed npm packages are not shared among different Node.js versions, as this could cause incompatibilities. Rather, they’re installed alongside the current Node version in ~/.nvm/versions/node/
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, for example to uninstall Node.js version 11.10.1, run the command: nvm uninstall 11.10.1
Switching Between Node.js Versions
To switch to a different version of Node.js, use the nvm command use followed by the version of Node.js you would like to use:
nvm use 0.10
This is the output you will see:
Output
Now using node v0.10.48 (npm v2.15.1)
You can even switch back to your default version:
nvm use default
At this point, you have installed several versions of Node.js. You can use nvm to uninstall any unwanted version of Node.js you may have.
Removing Node.js Versions
You may have several versions of Node.js installed due to working on a variety of projects on your machine.
Fortunately, you can remove Node.js versions just as easily as you installed them:
nvm uninstall 0.10
This is the output that will display after running this command:
Output
Uninstalled node v0.10.48
Unfortunately, when you specify a major or minor version, nvm will only uninstall the latest installed version that matches the version number.
So, if you have two different versions of Node.js version 6 installed, you have to run the uninstall command for each version:
It’s worth noting that you can’t remove a version of Node.js that is currently in use and active.
You may want to return to your system’s default settings and stop using nvm. The next step will explain how to do this.
Unloading Node Version Manager
If you would like to completely remove nvm from your system, you can use the unload command:
nvm unload
If you would still like to keep nvm on your system, but you want to return to your system’s installed version of Node.js, you can make the switch by running this command:
nvm use system
Now your system will return to the installed version of Node.js.
Conclusion
Working on multiple projects that use different versions of Node.js doesn’t have to be a nightmare. Node Version Manager makes the process seamless. It enables a concern-free installation and easy switching between different versions, saving time for what really matters.