DEV Community

Mohammad Faisal
Mohammad Faisal

Posted on • Originally published at mdfaisal.com

Why NVM is Tremendously Helpful for Web Developers

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

If it’s successful you can check the version:

command -v nvm
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Check the installed versions

If you want to see the list of versions installed on your machine run the following command:

nvm ls
Enter fullscreen mode Exit fullscreen mode

This will show the versions on your machine.

result of npm ls

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
Enter fullscreen mode Exit fullscreen mode

You can check the version of the node by running the following command:

node -v
Enter fullscreen mode Exit fullscreen mode

And now you are using Node 14.

version switch of node

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
Enter fullscreen mode Exit fullscreen mode

And check the version:

npm --version
Enter fullscreen mode Exit fullscreen mode

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

Top comments (1)

Collapse
 
9opsec profile image
9opsec

The command below doesn't work for me on windows:

nvm install-latest-npm

But this does:
nvm install lts