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.
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:
If all is well, you will get the installed version of nvm.
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.
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
node --version again and now we're on Node.js v16.
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.
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.
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
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.
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!