nvm, the Easiest Way to Switch Node Environments on Your Machine in a Flash
Tags: javascript, node, nvm
Published: 2023-05-29 16:53:42

---
title: nvm, the Easiest Way to Switch Node Environments on Your Machine in a Flash
published: true
date: 2019-03-03 00:00:00 UTC
tags: javascript,nodejs,nvm
canonical_url: https://www.paigeniedringhaus.com/blog/nvm-the-easiest-way-to-switch-node-environments-on-your-machine-in-a-flash
---

[![nvm logo](https://www.paigeniedringhaus.com/static/8480b34286af029887260f120b63a5d3/8efd0/nvm-hero.webp "nvm logo")](/static/8480b34286af029887260f120b63a5d3/10884/nvm-hero.webp)

_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)](https://github.com/creationix/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](https://github.com/nvm-sh/nvm)

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`](https://github.com/nvm-sh/nvm#installation-and-update) 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:

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

Wget command:

```shell
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:

```shell
command -v nvm
```

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

[![Confirmation nvm is installed on the command line](https://www.paigeniedringhaus.com/static/84740927662b931c9fb5d13aa1db96f1/8efd0/nvm-installed.webp "Confirmation nvm is installed on the command line")](/static/84740927662b931c9fb5d13aa1db96f1/3d776/nvm-installed.webp)

_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](#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**](https://github.com/nvm-sh/nvm#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:

```shell
nano .zshrc
```

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

```shell
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](https://www.paigeniedringhaus.com/static/84740927662b931c9fb5d13aa1db96f1/8efd0/nvm-installed.webp "Confirmation nvm is installed on the command line")](/static/84740927662b931c9fb5d13aa1db96f1/3d776/nvm-installed.webp)

_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](https://www.paigeniedringhaus.com/static/73410060e69007849b3ea93d4bef69fd/8efd0/ls-remote.webp "List of Node.js versions available via nvm")](/static/73410060e69007849b3ea93d4bef69fd/bbe5b/ls-remote.webp)

_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](https://www.paigeniedringhaus.com/static/870120fc17a6d123aa24947b3c1e1d44/8efd0/nvm-ls.webp "All the versions of Node.js installed on my local machine")](/static/870120fc17a6d123aa24947b3c1e1d44/83a31/nvm-ls.webp)

_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](https://www.paigeniedringhaus.com/static/d53cbeae63f112b25ce0be721bde44b6/8efd0/nvm-install.webp "Install the latest Node version via nvm")](/static/d53cbeae63f112b25ce0be721bde44b6/3d776/nvm-install.webp)

_I just installed the latest version of Node.js to my local development machine._

```shell
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](https://www.paigeniedringhaus.com/static/3bfcf1b5de58a87b1acc9d2b3c529bd8/8efd0/nvm-use.webp "Choose which version of Node.js to use with nvm")](/static/3bfcf1b5de58a87b1acc9d2b3c529bd8/22e6c/nvm-use.webp)

_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](https://www.paigeniedringhaus.com/static/04056cc33c6e31d2eb562cb723658411/8efd0/nvm-run.webp "Choose which version of Node.js to run with nvm")](/static/04056cc33c6e31d2eb562cb723658411/22e6c/nvm-run.webp)

_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](https://www.paigeniedringhaus.com/static/44f74ac101abf8dae9fddb7827d676b5/8efd0/nvm-alias.webp "Give Node version an alias with nvm")](/static/44f74ac101abf8dae9fddb7827d676b5/588c1/nvm-alias.webp)

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](https://github.com/nvm-sh/nvm#table-of-contents) 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.

If you’d like to make sure you never miss an article I write, sign up for my newsletter here: https://paigeniedringhaus.substack.com

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

- [nvm, GitHub](https://github.com/nvm-sh/nvm)