Andy Crouch - Code, Technology & Obfuscation ...

Using NVM To Manage Node Versions Across Projects

Photo: Unsplash - Brennan Angel

A lot of the projects I am picking up as part of my work recently has been around Node and JavaScript. This ecosystem is one that I have studied from the sidelines. While I have used JavaScript for well over 15 years, it has always been in the browser. So some of the future posts here over the next few weeks will be useful snippets and brain dumps of stuff I am learning.

One thing that most developers agree on is the speed at which the JavaScript community moves. The rate at which package versions are upgraded and at which frameworks change can cause problems for one project. If you are managing more than one project then you need a way to manage multiple versions of Node. The answer is to install NVM - Node Version Manager.

How you install NVM depends on your platform. The Github page has detailed instructions that I will not repeat here. Once installed you will need to source the NVM file from your shell rc file (.bashrc, .zshrc etc):

source /usr/share/nvm/init-nvm.sh

(Note - The installation path may vary on your machine).

(nvm is not natively supported on Windows but there is an NVM-Windows package.)

Using nvm is really easy:

To install a new Node version for the local user you can execute nvm install version:

nvm install 12.13.0
nvm install 11.10.1

To switch between Node versions then you execute nvm use version:

nvm use 12.13.0
nvm use 11.0.1

It really is that simple.

If you want to force a given version for a project then you can create a .nvm file in the root directory of the project. In the file, you just add the Node version number the project should use wrapped in single quotes. Then when you switch to the project you can just run:

nvm use

The project Node version will be used.

If you want to use nvm to see what versions of Node are available then you can use:

nvm ls-remote

Nvm also has some aliases which target the latest versions of both the stable and unstable:

node 
unstable

So running the following will install the latest version of Node:

nvm use node

You can, of course, uninstall versions using:

nvm uninstall 11.0.1

If you want to check the version of node you are running you can use

nvm current.

Nvm is a really useful tool to manage Node versions. I hope to see more people using it to set Node version information at a project level. This will speed up the process of getting a project running locally.

If you have thoughts around this subject please contact me via twitter or email.