Managing multiple node versions with NVM

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
NVM is great indeed, thank you for putting this together Chris Bongers.
I use this tiny shell script in my .zshrc file that looks for a .nvmrc file, and automatically switches to the node version defined there, or falls back to the default one if can't find the file.
autoload -U add-zsh-hook
load-nvmrc() {
local node_version="$(nvm version)"
local nvmrc_path="$(nvm_find_nvmrc)"
if [ -n "$nvmrc_path" ]; then
local nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")")
if [ "$nvmrc_node_version" = "N/A" ]; then
nvm install
elif [ "$nvmrc_node_version" != "$node_version" ]; then
nvm use
fi
elif [ "$node_version" != "$(nvm version default)" ]; then
echo "Reverting to nvm default version"
nvm use default
fi
}
add-zsh-hook chpwd load-nvmrc
load-nvmrc
It will help those who don't use VS Code 🙂
What are your thoughts on Volta as an alternative?
Lars Gyrup Brink Nielsen can share his expert opinion about Volta, and this reminds me that I have to try it as well 😆
Umair Hafeez Give it a crack mate, it's just as easy to use, but more convenient!
Hi, Umair Hafeez!
Yes, I use Volta. It is better than NVM in all aspects:
This is so helpful, I’ve been wanting to use this but always get stuck on editing the profile. Thank you!
Happy to hear it helps Hunter
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

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.
The easiest way to install NVM is by using Homebrew.
Run the following command:
brew install nvm
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
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
This should output the version of nvm you are using.
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
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
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
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!
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter