DEV Community

Cover image for Managing multiple node versions with NVM
Chris Bongers
Chris Bongers

Posted on • Originally published at daily-dev-tips.com

Managing multiple node versions with NVM

While working on multiple projects, some of them may use different node versions.

This can be a real pain point when you accidentally ran npm install or npm update with a different npm version.

To address these issues, we get something unique called nvm.
It stands for: Node Version Manager.

And it can be used to switch between different node versions on your local machine quickly.

Installing NVM

The easiest way to install NVM is by using Homebrew.

Run the following command:

brew install nvm
Enter fullscreen mode Exit fullscreen mode

Once installed, you must add it to your preferred profile file so we can use it globally.
A profile file can be one of these: (~/.bash_profile, ~/.zshrc, ~/.bashrc).

Once you have identified it, add the following lines.

export NVM_DIR=~/.nvm
source $(brew --prefix nvm)/nvm.sh
Enter fullscreen mode Exit fullscreen mode

This will ensure that you can run the nvm commands every time a terminal opens.

We can quickly test if it works by running the following command in a terminal.

nvm -v
Enter fullscreen mode Exit fullscreen mode

This should output the version of nvm you are using.

Installing and using different node versions

The idea behind nvm is that we can install and manage multiple node versions.

The first step is to install the versions we might need.

nvm install 16

nvm install lts

nvm install 12.14.3
Enter fullscreen mode Exit fullscreen mode

The above are all accepted node versions you can install.

Once we have them installed, we can use the following command to use them.

nvm use 16

nvm use lts

nvm use 12.14.3
Enter fullscreen mode Exit fullscreen mode

Pro tip

Add a .nvmrc file to the root of your project. In there, add the version of the node this project uses.

For instance, a file could have node 12.14.3. We can then add the .nvmrc file and the following content.

12.14.3
Enter fullscreen mode Exit fullscreen mode

Now when you open the project in your favorite editor, you can run the nvm use command, and it will install the version defined in the .nvmrc file.

Bonus tip:
A great visual studio code plugin does this every time you open a project!

Download the NVM VSC plugin

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Top comments (3)

Collapse
 
dailydevtips1 profile image
Chris Bongers

Oh nice, first time seeing this actually for me.

Collapse
 
damian_cyrus profile image
Damian Cyrus
Collapse
 
murtuzaalisurti profile image
Murtuzaali Surti

For Windows OS, you can use nvm-windows! It's not official but it gets the work done! Nice article!