To read more articles like this, visit my blog
If you work in the Node.js ecosystem, you’ve probably heard about the Node version manager (NVM).
Today, we will see why this exists and why you should care about learning it.
What is NVM?
According to the official docs,
nvm
allows you to quickly install and use different versions of Node.js via the command line.
Apart from that, it also keeps you away from many errors.
The problem at hand
Have you ever encountered the EACCESS
error when installing a global npm package?
npm install -g some-package
I bet you have! But what’s the solution? Some StackOverflow answers suggest you use sudo
, which is a very bad idea and can lead to more problems.
The official npm docs suggest that we use node version manager (nvm)
to resolve this issue.
Install NVM
If you are using Linux, run the following command:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
If it’s successful you can check the version:
command -v nvm
You may have to close your terminal and reopen it again.
Install the Latest Version of Node.js
If we want to install the latest version of the node we can run the following command:
nvm install node
At the time of writing this article, the latest version of the Node.js is v17.3.0
.
Install Specific Version of Node.js
At the time of writing this article, the latest version of Node.js is 16.13.1
. If we want to install this specific version:
nvm install 16.13.1
Check the installed versions
If you want to see the list of versions installed on your machine run the following command:
nvm ls
This will show the versions on your machine.
Use a specific version of the node
Now let’s say you have multiple versions of the node installed on your machine. And you want to switch between them. Maybe you want to work on a legacy app that uses Node.js 14.
You can just run:
nvm use 14
You can check the version of the node by running the following command:
node -v
And now you are using Node 14.
Install the Latest Version of npm
You can install the latest version of npm for the version run the following command:
nvm install-latest-npm
And check the version:
npm --version
So essentially you can have multiple versions of npm on your machine now!
How cool is that?
Final Notes
These tools are here to make our lives easier. Let’s take full advantage of them.
Hope you learned something new today. Have a great day!
Have something to say? Get in touch with me via LinkedIn or Personal Website