Node Version Management... Automatically
Tags: nvm
Published: 2020-02-26 22:19:59

---
title: Node Version Management... Automatically
published: true
date: 2020-01-24 00:00:00 UTC
tags: nvm
canonical_url: https://stephencharlesweiss.com/blog/2020-02-14/nvm-node-version-management-automatic/
---

NVM, the [Node Version Manager](https://github.com/nvm-sh/nvm), is an invaluable tool when working on multiple node projects. Instead of manually managing the globally installed version of node, NVM can handle it for you.

Unfortunately, even with NVM, it’s easy to use the wrong node version when jumping between projects.

A colleague shared his solution to the problem with me recently:

```
//".bash\_profile"
function cd() {
    builtin cd "$@"
    nvm i 2> /dev/null
}
```

This function, when added to the `.bash_profile` (or `.zshrc`, etc.), will still perform the change directory as expected (that’s the first line: `builtin cd "$@"`), but it will _also_ look for the presence of an `.nvmrc` file in the new directory.

## [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#quick-aside-what-is-the-nvmrc)Quick Aside: What Is The .NVMRC?

It’s worth taking a moment to discuss the [`.nvmrc`](https://github.com/nvm-sh/nvm#nvmrc) file, it’s function, and what it may look like:

> You can create a .nvmrc file containing a node version number (or any other string that nvm understands; see nvm —help for details) in the project root directory (or any parent directory). Afterwards, nvm use, nvm install, nvm exec, nvm run, and nvm which will use the version specified in the .nvmrc file if no version is supplied on the command line.

So, for example, you could have an `.nvmrc` for a specific version:

```
//.nvmrc
12
```

_Or_ you could default to the latest LTS version:


```
//.nvmrc
lts/*
```

_If_ there’s an `.nvmrc` included in the new directory, the function will execute ` /dev/null`.

| [![cd with extra function](https://stephencharlesweiss.com/static/00e7c819144dc010328c2bbae6b72331/c08bc/cd-in-practice.png)](/static/00e7c819144dc010328c2bbae6b72331/c08bc/cd-in-practice.png) |
| --- |
| An example of this in use. The directory, `personal`, does not have an `.nvmrc` file, though `onething` does. Upon detecting it, `nvm` automatically switched to using the correct `node` version. |

As noted in the description of the `.nvmrc`, it’s presence ensures that `nvm i` will use the version specified in the file.

The ` /dev/null` is a specific form of [redirection within Unix](../../2019-12-20/angled-brackets-bash-scripting).

In this case it’s saying take the `stderr` (`2`), redirect (`>`) to `/dev/null`. The [`/dev/null` is a “blackhole”](https://askubuntu.com/a/350212) on unix machines, effectively suppressing any errors generated by running this command.

## [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#final-note)Final Note

NVM manages node versions _within_ each terminal session. This is critical because it ensures that you can run two node projects simultaneously even if the node versions are different.