JavaScript

How To Switch Between Node.js Versions Using nvm

Harry 4 min read
How To Switch Between Node.js Versions Using nvm
Table of Contents

When working with multiple projects, or you've pulled down a repository from another developer with a different setup from yours. Then you cannot always depend on running the same version of Node.js between them all.

It would be quite a faff if you had to uninstall and install the version of Node.js required each time you switched projects.

This calls for the need to run multiple versions of Node.js on your local machine. Short of using a containerised environment with Docker, you can use nvm (Node Version Manager) to fill in this gap.

GitHub - nvm-sh/nvm: Node Version Manager - POSIX-compliant bash script to manage multiple active node.js versions
Node Version Manager - POSIX-compliant bash script to manage multiple active node.js versions - GitHub - nvm-sh/nvm: Node Version Manager - POSIX-compliant bash script to manage multiple active nod…

Installing nvm

The first thing to do is visit the GitHub page where nvm is hosted. Here you will find the setup instructions to install nvm.

But in short, it involves running one of the following two commands.

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash

Once you have completed the installation step, you may need to reload your terminal/command line. Then check everything is ready to go, try printing out the version of nvm installed:

nvm --version

If all is well, you will get the installed version of nvm.

nvm --version
ℹ️
The version of nvm does not relate to the version of Node.js you have installed.

Installing and using Node.js

Now all we need to do is pick a version of Node.js to install. Which is... as simple as a single command:

nvm install node

This will go ahead and install the latest version of Node.js available.

You can also select a version of Node.js to install by specifying the version. So if you want to install Node.js v18, you would do the following.

nvm install 18

After you have Node.js installed. Let's give it a try to make sure everything is in working order.

node --version
node --version

Now I did say nvm allows you to switch Node.js versions about, right? Go ahead and install another version of Node.js by specifying a different version to the one you installed above.

For example, Node.js v16:

node install 16

If you repeat the same node --version command from above. You will notice the version didn't swap.

Let's fix that.

nvm use 16

Then calling node --version again and now we're on Node.js v16.

switching node.js

Sorted!

Installing LTS Versions of Node.js

If you intend to stick on a single version of Node.js longer than intended, then using an LTS (long-term support) version will ensure you receive important security updates for a total of 30 months.

Releases
Releases | Node.js

Every even-numbered release will enter a maintenance window during this time, where it will receive bug fixes and security updates. If you're rolling a project into production this it is recommended to use one of these versions of Node.js.

To install the latest LTS version using nvm, you can pass in the --lts option when installing Node.js.

nvm install --lts

Listing All Installed Versions of Node.js

If you lose track of all the versions of Node.js you have installed. Run the following command to see a list of all installed versions, with a -> showing which version is currently active.

nvm ls
nvm ls

Setting Default Node.js Version

When you close and open your terminal, you may find your version of Node.js has changed or even unlinked altogether. This means you need to call nvm use x again. It's not a huge issue but it's annoying so let's sort this out with the following.

nvm alias default 18
nvm alias default

What this will do is set your default Node.js to the version you specify. So if you close and open your terminal again, the intended version of Node.js will be enabled right away.

Setting Node.js Across Projects and Teams

So far we've been providing the version of Node.js we've wanted ourselves. But if we wanted to go further and set a specific version across a project or to share with team members. We can use what is called a .nvmrc file and commit this alongside the codebase.

A quick example of what one of these looks like.

.nvmrc file

In my project above I have a .nvmrc file with the version of Node.js specified (v16). Next when I run nvm install and nvm use without specifying a version upfront, it'll look for a .nvmrc file and install/change the specified version.

Super handy for when you are working in teams!

Share
Comments

Harrk Blog

Programming nonsense, tutorials, and random musings about games and technology.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Harrk Blog.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.