There are few ways to install Node on your local machine. The most popular way is to install it following official website instructions. But if you use this way you will install just one specific (latest) version of Node. What if you need to install a specific version of Node? Or you need to upgrade from one version to another but only for a short while.
For that purpose, you can use a tool called nvm (Node Version Manager).
Because I use macOS on my local machine these instructions are specific to that operating system. You can find instructions that are specific to an operating system that you use on official documentation on GitHub.
To install nvm on your local machine let's use this command:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
Now let's type the command which tells us that nvm is installed and available on our local machine:
You should see the current version of Node Version Manager that is installed on your local machine.
fish: Unknown command: nvm
Currently, I use Fish Shell. If you use something else you should check detail information about troubleshooting on GitHub.
fisher install FabioAntunes/fish-nvm edc/bass
Now, if you type
nvm --version you should see a version of Node Version Manager that is installed on your local machine.
To install latest version of Node you can use the command:
nvm install node
To see all versions of Node that are installed on your machine use the command:
It should print in the Terminal something like this:
-> v16.1.0 system default -> node (-> v16.1.0) iojs -> N/A (default) unstable -> N/A (default) node -> stable (-> v16.1.0) (default) stable -> 16.1 (-> v16.1.0) (default) ...
Pay attention to the symbol "->". It shows us which version of Node is current on the local machine. So, basically, when you type
node -v you should see the same version as with "->" before (in that case this version is 16.1.0).
Now let's install another version of Node. Let's say, I want to use some older version of Node. For example, 14 version. To install it use the command:
nvm install 14
It should install Node v.14.16.1. Let's look at the list of installed Node versions (nvm ls):
-> v14.16.1 v16.1.0 system default -> node (-> v16.1.0) iojs -> N/A (default) unstable -> N/A (default) node -> stable (-> v16.1.0) (default) stable -> 16.1 (-> v16.1.0) (default) ...
Now we have two versions: 14.16.1 and 16.1.0. The current is 14 (the symbol "->" before).
To switch to version 16 use this command:
nvm use 16
We should see the answer of nvm:
Now using node v16.1.0 (npm v7.11.2)
And if we use the command nvm ls again we will see that the current version is 16.1.0.
To uninstall latest version of Node use command:
nvm uninstall node
To uninstall specific version of Node (for example - 14) use command:
nvm uninstall 14
Global npm packages
If you have some installed globally npm packages you should notice one thing. When you installed this npm package on one version of Node and then you switch to another version of Node, the installed npm package won't be available for you. It's because npm packages that were installed on different versions of Node located in different places.
npm i -g prettier
And then we switch to version 14:
nvm use 14 prettier --version
You will see something like this:
prettier: command not found
Don't worry! If you want to use that package on version 14 you just need to install it again.
Packages are located here (on macOS):