Manage you node versions with NVM

What is nvm?

Nvm (node version manager) is a tool for (you guessed it) managing different version on node on your machine. The great thing about Nvm is that it lets you rapidly switch between those different version. You could even add a .nvmrc file at the root of your project making sure fellow nvm’ers are using the correct node version for your project.

Here’s a quick guide on how to install it using homebrew:

To install nvm, first run brew install nvm.

Check your nvm version running nvm -v.

If the console prints out a version number, continue to the next step. If not, you could try googling the console message, or something like ‘how to install nvm using home brew’. There are plenty of in depth guides out there!

Next, to install the latest stable version of node, run nvm install lots

Then, to view your current installed version of node, run nvm ls.

To list all available node versions, run nvm ls remote.

Note that these versions are not yet installed locally on your machine, but they’re available for installation.

To install a specific version of node (I’m using v12 as an example here, you can change this to any available version number) run nvm install v12.

Run nvm ls and confirm that v12 is added to the list of installed node versions.

We now have two version of node installed, and this is where nvm shines.

To switch to our newly added node version, simply run nvm use v12.

If you want to set this as the default node version, run nvm default command

So, we have now installed nvm, added two versions (LTS and v12) and changed the default to v12.

Let’s create a new folder with a .nvmrc file, telling nvm which version of node we would like to use inside the current folder.

Create a new folder (or directory if you want) by running mkdir nvm-fun

Remember that it’s good practice to keep your folder names free from spaces.

To create a .nvmrc file with some text in it, run echo >> "nvm version" nvm-fun/.nvmrc.

Run cd nvm-run to navigate into our folder.

Run node -v.

Notice something different?

Your node version has changed to the version we specified in our .nvmrc file. This comes in handy in cases where you work on multiple projects containing different versions of node.

In stead of switching manually every time you cd into the projects folder, you could simply create a .nvmrc file in the project root, and specify the preferred version in it.

There’s some other interesting stuff you could do with it as well, check out nvmcdocs for more of an in-depth overview.