How to install Node Version Manager (nvm)
Abdulahi Roble / March 26, 2022
3 min read
I came across a project which was using an older version of node and in order to run it successfully I would have to uninstall the latest version of node and install the older one.
Reason for that is because there were some packages in the project that were not compatible with the newer version.
The issue with that is let's say I had a different project which were using a later version of node.
Then I would have to go through the same steps again of uninstalling the older version and installing the newer one and that would be a lot of work and also time-consuming.
This is where using nvm came in pretty handy because it allowed me to install multiple versions of node.js and manage them easily.
Nvm which stand for Node Version Manager essentially allows you to quickly install and use different versions of node via the command line.
This is especially useful since you don't have to uninstall and reinstall both node and npm every time you are working with a different project which runs a different version of node.
So how do we go about installing nvm on our machine? For this tutorial I'm going to use a Macbook but if you have a Windows machine you can check out this tutorial from YouTube to learn how install it on there.
Before we begin it might be a good idea to uninstall any existing version of node so that it does not conflict with the one we are going to install.
Run the install script
You can find the nvm repository through this link.
In order to install nvm on our machine we need to run the install script.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
This will clone the nvm repository to ~/.nvm and then to add the source lines from the snippet below to the correct profile file.
In my case it is ~/.zshrc but if you are using bash it would be ~/.bashrc or ~/.bash_profile.
To check if nvm is successfully installed we can run the following command.
❯ nvm --version
0.34.0
How to run and use different node versions
❯ nvm use 16
Now using node v16.9.1 (npm v7.21.1)
❯ node -v
v16.9.1
❯ nvm use 14
Now using node v14.18.0 (npm v6.14.15)
❯ node -v
v14.18.0
❯ nvm install 12
Now using node v12.22.6 (npm v6.14.5)
❯ node -v
v12.22.6