Learn about what NVM is and why you should use NVM to update and manage Node.js on your computer.
What is NVM?
NVM stands for Node Version Manager. NVM allows you to manage, update and switch between numerous active Node.js versions installed on your computer.
Why is it a good idea to have multiple versions of Node on your machine, doesn't it get confusing or cause conflicts?
There can be many reasons that different projects use different versions of Node. What’s important is that you as a developer can painlessly work on multiple projects with different Node versions on your machine.
There’s a good reason that Node.js official website offers two versions of Node:
- Left: the latest stable version (LTS), recommended for most users.
- Right: the current version, only recommended for experienced developers.
LTS stands for Long Term Support. It’s the most stable version of Node available. The LTS version of Node has long term support by the maintainers, which is important, because in case that a critical fix is required (like security issues) you can expect the Node.js team to act on it quickly.
If you’re using the latest current version of Node.js you get access to the newest features — but at the same time, you’re at risk of running into unpredictable issues, because unlike the LTS, the current version can be unstable.
Why NVM is awesome
NVM allows you to run as many versions of Node on your machine as you need, without causing code conflicts.
By using NVM to manage Node.js:
- You can experiment with the latest current version of Node.js (with the newest features) risk-free when you work on your projects.
- You can use the latest stable version for client projects.
- You can use any previous version of Node.js to run a specific project, e.g. one that you found on GitHub.
And without the different Node versions interfering with each other.
Note: Never use an unstable version of any software on a client’s app or website unless they specifically ask for it (unlikely).
Install NVM
To install NVM, open your Terminal and run one of the following commands.
curl
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.2/install.sh | bash
wget
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.2/install.sh | bash
curl
and wget
are different tools. Use whichever fits your setup. I use curl
.
Update NVM
To get the latest stable version (LTS), use this command:
nvm install lts/* --reinstall-packages-from=node
To get the latest current version of NVM:
nvm install node --reinstall-packages-from=node
Again, for beginners, go with the LTS version.
Install Node with NVM
Pay attention to the difference between the following commands that either install the beginner-friendly LTS (Long Term Support) version or the latest current version of Node (with the newest features), for experienced users.
If you’re a beginner, just stick with the LTS.
Install the latest Node.js LTS version with NVM
To install the latest stable version (LTS) of Node, run this command:
nvm install --lts
To use it inside your current shell run this command:
nvm use --lts
If the use
command doesn’t work, restart your terminal and run the command again.
Install latest current Node.js version with NVM
To install the latest current version of Node run this command:
nvm install node
Run this command to enable Node:
nvm use node
If it doesn’t work, restart your terminal and run the command again.
Check your versions
To confirm that you are using the desired version of Node run this command:
node -v
Tip: a common reason that Node-based projects suddenly stop working (without you directly changing anything inside the project) is that you’re running the wrong Node version for that project.
Get used to checking your versions, and avoid a lot of unnecessary frustration.
Install specific Node.js version with NVM
Let’s say that you need to install a very specific version of Node for a project, e.g. 12.15.1
. Run this command:
nvm install 12.15.1
And then enable it:
nvm use 12.15.1
Note: When you want to install a specific Node version, you don’t have to worry about whether you’re installing the LTS or Current. NVM automatically finds out based on the version number.
Change Node versions
Switch between different Node versions and use it inside your current shell:
nvm use xx.xx.x
Fill out xx.xx.x
with whatever version you need to switch to.
Remember, if you haven’t previously installed the specific version of Node that you need, then you can’t just switch to it. You have to install it first, and then enable it inside your shell, as you saw in the previous segment.
List all installed Node versions:
This command will give you an overview of all the Node versions you have currently installed on your machine:
nvm ls
Remove specific Node version:
This command removes a specific Node version from your machine:
nvm uninstall xx.xx.x
There’s no reason to have unused Node installations taking up space.
Set default Node version:
To decide which Node version should be your shell default, use this command:
nvm alias default xx.xx
Now, whenever you open a new shell in your terminal, that shell will use whatever Node version you set as default.
Important
Apropos default versions. Beware of this common scenario:
- You work on a project that uses a different Node version than your set default.
- It’s Friday, at 5 o'clock. You shut down your terminal/computer because it’s weekend and you need to cool down and have fun with your friends and family.
- You return to work Monday. But now your shell is set to the default Node version, (which you forgot all about) which is incompatible with that specific project. You launch your server and BOOM...
- You: “WTF my project stopped working for no reason?!?!?!”
Chances are that there’s a reason, and often it’s because of version conflicts.
All you need to do to fix this problem is to manually change the Node version inside your shell (for that project) back to whatever is the right version for that specific project, by using the command you just learned about:
nvm use xx.xx
NVM is such a massive time (and frustration) saver. Once you start using it you’re going to be like:
Because you can’t know what you don’t know — but now you know.