First published March 3, 2019 in ITNEXT

nvm, the Easiest Way to Switch Node Environments on Your Machine in a Flash

Nope, I didn’t misspell npm — Node Version Manager is its own handy dev tool.

nvm logo

nvm — a simple bash script to manage multiple active Node.js versions.

Multiple Node environments are a pain to develop locally

I'm sure I’m not alone when I tell you that my current development team owns two different UI applications: one built in AngularJS (the old one) and one built in React (the new one). The two work together to serve up a single user experience, while we slowly migrate over the existing screens and functionality from the old, AngularJS application into the new, React application. The end goal is that the React application will one day host the entire application on its own.

I’m sure I’m also not alone when I tell you that the AngularJS application will ONLY run on Node.js version 9 (it crashes and causes weird bugs if it’s not), and our React application needs Node version 10 or above to take advantage of all the ES6 and beyond features.

And you know what? Switching between Node environments for local development is kind of a pain. It’s not easy, it’s something I forget to do frequently (until I have an unexplained issue during development), and frankly, it’s just not the easiest thing to do on a Mac.

Painfully changing Node versions was my lot in life, until a co-worker clued me in to an awesome tool called Node Version Manager (nvm).

nvm is a local web development game changer. Let me tell you how.

Node Version Manager

What is it?

Node Version Manager is exactly what its name says:

[nvm is a] Simple bash script to manage multiple active node.js versions. —nvm, GitHub

While it doesn’t sound complicated, what nvm can do is awesome. It makes it possible to:

  • Access every long term support (LTS) version of Node.js from v0.1.14 to the latest version today, which happens to be v.11.10.1, as I write this.
  • Download any one of those remote LTS versions of Node locally with a simple command.
  • Set up aliases to switch between different downloaded versions of Node with ease.
  • Default to automatically use whichever version of Node.js is specified if a .nvmrc file is present in a repo.
  • And it works with multiple types of shells: Sh, Bash, Zsh, Dash, Ksh, (not Fish though).

As long as you’re fairly comfortable with the terminal, you can use NVM.

Setting up nvm

nvm is relatively easy to set up too — after hearing about its benefits, I was able to use the GiHhub README.md to set it up on my computer in short order.

Step 1: Install nvm

The first step is simplest: just install nvm with the curl or wget command provided in the documentation.

cURL command:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash

Wget command:

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash

Step 1.5: Verify nvm in the command line

Close your current terminal for the install to take effect, open a new terminal window and type:

command -v nvm

If it’s installed you’ll get a message like the screenshot below.

Confirmation nvm is installed on the command line

This is the nvm response you want to see. If you get a response like `nvm command not found`, it’s not ready to be used yet.

If the version of nvm shows up in the terminal, you’re ready to go and can move on to the section Using nvm.

If you get an error, you’re like me and need to do a bit more manual installation to set up your shell to point to nvm's home directories. Keep reading.

Step 2: Add the nvm directory paths to your shell profile (when needed)

For me, even after installing nvm using the cURL command, I still got an error message in my terminal when I typed the command -v nvm to verify the installation.

At that point, I jumped down the documentation to the Git Install section which had additional notes on how to add the nvm directory paths to the various shell profiles like ~/.bashrc or ~/.zshrc, in my case, since I prefer to use Zsh.

To edit my .zshrc, I just run the following command in the terminal:

nano .zshrc

Scroll down to the bottom of the file and paste in the following lines.

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

Then type CTRL + X from a Mac, Y to save the changes, and Enter and you should be back where you started in the terminal.

If you want to double check your changes are there, you can run cat .zshrc and scroll down with the down arrow key to check the new nvm scripts are at the bottom of the file.

Once you’re sure it’s there, completely quit the terminal program and reopen it and type command -v nvm again, and you should see this:

Confirmation nvm is installed on the command line

Now you’re in business. nvm is installed and ready to be used.

Now we’re ready to use nvm.

Using nvm

nvm is super simple to use. Here’s the commands you’ll need to know when you want to work with nvm in the command line.

nvm ls-remote

This shows you all available LTS versions of Node.js.

List of Node.js versions available via nvm

This is just a fraction of all the available Node.js versions nvm has to offer.

nvm ls

Shows you all installed versions available locally on your machine.

All the versions of Node.js installed on my local machine

The versions of Node.js installed on my local machine.

nvm install node OR nvm install 8.15.1

Typing nvm install node will install the latest version of Node.js to your machine, or nvm install <SPECIFIC_NODE_VERSION> will install the specified version.

Install the latest Node version via nvm

I just installed the latest version of Node.js to my local development machine.
nvm use node OR nvm use 11.10.0

This command will set the version of Node.js running locally to the latest version downloaded if you just type nvm use node, or the version specified if you append the command with the version number nvm use --version e.g. nvm use 8.15.1.

Choose which version of Node.js to use with nvm

Both of the use commands: the first defaults to the latest version (11.10.1), the second sets the version to 11.10.0.

NOTE: The version you want to run locally must be installed on your machine first, or else Node will fall back to whatever version it was running before the command was issued.

nvm run node OR nvm run 11.10.0

This command is almost the same as the one above, the only difference is typing nvm run node or nvm run --version like nvm run 11.10.0 will switch to the specified version of Node.js and open up a Node command line for you to run commands manually from afterwards.

Choose which version of Node.js to run with nvm

From nvm run node, I can then run commands like `console.log(‘hello world’)` or `server.js` right after switching to a Node version.

In this way, you could, potentially, have multiple terminals open running multiple versions of Node at once. 🤔 Pretty handy…

nvm alias default node OR nvm alias default 11.10.0

These commands will set an NVM alias called ‘default’ to the latest downloaded version of node with nvm alias default node, or the specified version with nvm alias default --version like nvm alias default 11.10.0.

Once this default alias is set, any new shell will default to running with that version of Node.

Give Node version an alias with nvm

These are the main commands you’ll probably use to download and switch between Node.js versions while doing local web development. At least, they’re the ones I use most of the time.

The nvm documentation though, is pretty good and it goes into real depth if you want to get fancy with your nvm-ing.

Conclusion

Node versions are something we rarely think about until they become a problem during development. And if your situation is at all similar to mine, you may need to switch between multiple versions regularly, because your various web apps demand it.

Node Version Manager makes it incredibly easy to do this right from the command line. From installation to actual use, nvm is simple, and it makes development in whatever version of Node.js that’s required, so much simpler as well. I hope you’ll take advantage of it.

Check back in a few weeks — I’ll be writing more about the useful things I learned while building this project in addition to other topics on JavaScript, React, IoT, or something else related to web development.

Thanks for reading, I hope nvm can be useful to you and your development team when you’re writing your various JavaScript applications.

References & Further Resources

Want to be notified first when I publish new content? Subscribe to my newsletter.