DEV Community

loading...
Cover image for How To Install or Update Node by Using nvm (Node Version Manager)

How To Install or Update Node by Using nvm (Node Version Manager)

kowalevski profile image Max Kowalevski Originally published at kowalevski.com Updated on ・4 min read

Subscribe to my email newsletter to stay up to date.

subscribe

Intro

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).

Installation

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

Now let's type the command which tells us that nvm is installed and available on our local machine:

nvm --version
Enter fullscreen mode Exit fullscreen mode

You should see the current version of Node Version Manager that is installed on your local machine.

Shell Troubleshooting

If you use some shell as Z shell or Fish Shell you probably will have something like this in the Terminal:

fish: Unknown command: nvm
Enter fullscreen mode Exit fullscreen mode

Currently, I use Fish Shell. If you use something else you should check detail information about troubleshooting on GitHub.

Unfortunately, nvm doesn't support Fish. However, there is a solution called fish-nvm. It is a wrapper for Fish Shell. You can install this by using Fisher (Fish Shell Plugin Manager):

fisher install FabioAntunes/fish-nvm edc/bass
Enter fullscreen mode Exit fullscreen mode

Now, if you type nvm --version you should see a version of Node Version Manager that is installed on your local machine.

Usage

Node Installation

Now it's time to start using Node Version Manager to install Node on the local machine.

To install latest version of Node you can use the command:

nvm install node
Enter fullscreen mode Exit fullscreen mode

To see all versions of Node that are installed on your machine use the command:

nvm ls
Enter fullscreen mode Exit fullscreen mode

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)

...
Enter fullscreen mode Exit fullscreen mode

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

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)

...
Enter fullscreen mode Exit fullscreen mode

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

We should see the answer of nvm:

Now using node v16.1.0 (npm v7.11.2)
Enter fullscreen mode Exit fullscreen mode

And if we use the command nvm ls again we will see that the current version is 16.1.0.

Uninstall Node

To uninstall latest version of Node use command:

nvm uninstall node
Enter fullscreen mode Exit fullscreen mode

To uninstall specific version of Node (for example - 14) use command:

nvm uninstall 14
Enter fullscreen mode Exit fullscreen mode

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.

An example. Now I am on version 16 of Node. I want to install Prettier globally on my computer by npm:

npm i -g prettier
Enter fullscreen mode Exit fullscreen mode

And then we switch to version 14:

nvm use 14
prettier --version
Enter fullscreen mode Exit fullscreen mode

You will see something like this:

prettier: command not found
Enter fullscreen mode Exit fullscreen mode

Don't worry! If you want to use that package on version 14 you just need to install it again.

npm packages are located in specific folders for each version of Node that is installed by nvm.

Packages are located here (on macOS):

~/.nvm/versions/node/<version>/lib/node_modules
Enter fullscreen mode Exit fullscreen mode

Do you like the material? Please, subscribe to my email newsletter to stay up to date.

subscribe

Discussion (0)

Forem Open with the Forem app