How to change Node.js version with NVM?
NVM stands for Node Version Manager and its used to install and manage different Node versions. NVM allows developers to download multiple versions of Node and to keep track of which version is being used and to switch when required.
How to install NVM and Node
This portion of the article goes over how to install NVM and Node for the first time on your machine. We will install NVM first.
Windows
If you are using windows, head over to the following Github repo:
https://github.com/coreybutler/nvm-windows#readme
and click on Download Now
. Then go ahead and download the latest exe
setup file and install the wizard.
Mac or Linux
Head over to the terminal and write the following command:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
or
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
You can use either of the above two commands. The above commands will clone nvm repository into /.nvm
into your device.
Restart the terminal and run the nvm
command, you should get the following:
Now, we have installed NVM on our machine. Now we will use NVM to install Node:
To install the latest version which is v18.12.1
at the time of writing this article, run the following command in your shell:
nvm install --lts
This will install the latest version on the machine and you see the following result:
Now let’s say maybe you have a creating a project and some packages require version 12 of Node. To install v12.22.7
do the following:
nvm install 12.22.7
Display all node versions:
In order to display all the versions of node currently installed on your machine run the following command:
nvm ls
This will show all the node versions you have installed. Obviously, we have two versions installed: (v18.12.1 & v12.22.7)
The arrow in the above screenshot shows us the current version in use on the machine. Obviously, I have only installed 2 versions. If you have more than two installed they will all be listed here.
Switching Node versions:
In order to switch the node version, so for example if I want to use the latest version instead, write the following command in terminal:
nvm use <version-number>
So essentially, if I wanted to used the latest version, I can simply write:
nvm use 18.12.1
and thats it!
In the picture, you can see I ran that command and then checked the nvm ls
and the arrow points to the version 18.12.1
(which means this is the version used by the machine)
Uninstalling Node Version
In order to uninstall / remove a Node version just run the following command:
nvm uninstall <version-number>
So this is how you can install NVM and managed multiple versions of Node using NVM.