Skip to main content

Command Palette

Search for a command to run...

Managing multiple node versions with NVM

Published
2 min read
Managing multiple node versions with NVM
C

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

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

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.

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

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

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

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

U

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 🙂

1
D

What are your thoughts on Volta as an alternative?

U

Lars Gyrup Brink Nielsen can share his expert opinion about Volta, and this reminds me that I have to try it as well 😆

1
D

Umair Hafeez Give it a crack mate, it's just as easy to use, but more convenient!

1
L

Hi, Umair Hafeez!

Yes, I use Volta. It is better than NVM in all aspects:

  • Proper cross-platform support (NVM doesn't have full Windows support)
  • No commands needed when a repo config is present. No nvm use, no nvm install.
  • Official GitHub Action: volta/cli-action
  • Fewer updates needed
3
H

This is so helpful, I’ve been wanting to use this but always get stuck on editing the profile. Thank you!

3
C

Happy to hear it helps Hunter

More from this blog

D

Daily Dev Tips

887 posts

Looking to get into development? As a full-stack developer I guide you on this journey and give you bite sized tips every single day 👊