Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

HF Rulez the UniverseHF Rulez the Universe
Disease
Welly welly welly well
development programming javascript js node npm

NVM - Every JavaScript Developers Best Friend

Posted May 21, 2020 01:27 AM
If you're a JavaScript developer and have been for some time, then you undoubtedly know the pains of returning to a project after a long time away from it and trying to start it up.  Your new version of Node isn't compatible with half of the modules your project is dependent on.  And naturally one of those modules at some point did an upgrade that isn't fully backwards-compatible, so upgrading it is a pain in its own right.

Enter NVM.  Through this article we'll look at what NVM is, and why it's a must-have in every JavaScript developer's toolkit. By the end of this article you'll have set up NVM, learned how to install specific versions (as well as easy ways to install current/LTS versions), and how to swap active versions of Node on the fly.


What Is Nvm and Why Should I Be Using It?

NVM stands for Node Version Manager, and it facilitates the installation, and switching of, multiple Node versions.  Through NVM you can have the latest version of Node installed and, as needed, install or activate older versions in order to work on projects that are not yet compatible for whatever reason.  Another way to look at it- NVM allows you to develop against the LTS branch (Long Term Support) while having access to the most current version for testing and keeping up with new changes.


Installing NVM

Linux, Mac, and Windows (WSL) Users:
Getting started using NVM is pretty quick and easy from the command line, if you're on Linux, Mac, or in a Linux distro through WSL.

Step 1. Run the install script
Execute the following from your terminal.  This merely downloads the install script found here and executes it.  If you don't have cURL installed you may either try the wget option, or install cURL.

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

At this point you'll likely need to close your terminal session and relaunch it.  The installer will have added some environment variables to your profile, and that won't be available until you're using a new terminal session.

We can test that NVM installed correctly by running the following command:

Code
nvm --version

At the time of publishing this blog post, the current NVM version is 0.35.3.  If you receive a version back then congratulations, you're all set!  Jump down to the "Installing & Switching Node Versions" section and we'll dig into actually using NVM.

Windows (non-WSL) Users:

The official NVM doesn't support Windows, but fortunately a spin-off project called NVM-Windows exists.  And my advice is to do your Node development on Windows via WSL.  But, if you insist on working in Windows without WSL, or are on a version of Windows without WSL support, let's get you going.

Installation is a breeze- head on over to the NVM-Windows releases page on Github and download the most recent installer.  I recommend downloading the "nvm-setup" option, as that will come with an integrated installer.

Once done (it should be very quick), open a new command prompt and issue the following command:

Code
nvm version

The output should be the same version listed on Github, for the installer you downloaded.  As of publishing this article that is version 1.1.7.  If you're all set, then great, let's move on!


Installing & Switching Between Node Versions


Right off the bat, let's take a look at the versions of Node we already have installed (that are managed by NVM):

Code
nvm list

You may very well see no installations of Node here, and that's okay.  Otherwise, if you do have some versions installed, we'll cover switching between them and installing others right now.

Installing versions of Node is incredibly simple with NVM.  The most common version of Node to install is LTS.  Using NVM (not NVM-Windows) we can install Node's LTS version without having to look up what that version number is.  You simply issue the following code:

Code
nvm install --lts

NVM-Windows users will, unfortunately, need to install LTS based on its version number (we'll cover that shortly).

Looking to install the latest version of Node?  Also, very easy!

NVM:
Code
nvm install node

NVM-Windows
Code
nvm install latest

Of course, we can also install any version of Node we need- regardless of whether it's the most recent or ancient.  If you're using NVM (not NVM-Windows), you can see a list of every installable version via the following:

Code
nvm ls-remote

How useful that list is, though, is debatable.  Chances are if you're doing this, you know what version you need.  If not, you can always check the Node website.  But, let's say we need an older version of Node- v10.20.1.  To install that specific version of Node, we use the install command as before, but specify the version.  Like so:

Code
nvm install 10.20.1

Swap that version out for whatever version it is you require.

Now the real power behind NVM is being able to use these versions.  So far we've installed a couple.  But that's easily done with Node directly.  So how can we swap?  We need to tell NVM which version to use.

Code
nvm use 14.0.0

The nvm use command accepts a version number that's been installed.  Again, use the nvm list command if you need a reminder to what you have installed on your PC.  The version you feed will, once executed, be the current version of node in use on your system.  Go ahead- give it a shot.  Just check the version of Node after changing.


Defining Node Versions on a Per-Directory Basis


So now we can install specific versions of Node and swap between them.  Doing so on a global level is one thing.  But often times we're needing to set specific versions of Node up for a project's needs.  Having to remember to switch to a specific version of Node, via NVM, for a project can be tedious.  And you may forget what version you need if you walk away from a project for too long.

Enter a useful tool: .nvmrc

This, unfortunately, is not available for those using NVM-Windows.  But for those developing on Linux/Mac machines, or via WSL, you can leverage this tool.

RC files are a common convention in Linux, and indicates the file specifies some run-time commands/constraints.  You'll see this convention all over the place in modern web development (eslintrc, prettierrc, babelrc, etc.).

When set up, an nvmrc file will simply tell NVM that this project requires a specific version of node.  And its syntax couldn't be easier: you just set its contents to the version number you need.

Say you want a project to be locked to Node version 8.17.0.  You'd create an .nvmrc file, and set its contents to:

Code
8.17.0

That's it.  Enter that directly in your terminal and issue the following command:

Code
nvm which

You'll be told one of two things- either that NVM found a request for a certain version, and it's using it.  Or that you need to install that version.  If you see the latter, just install that version, and try again.

Directories with a .nvmrc file set up will tell NVM to use a specific version of Node.  All you need to do is issue the nvm use or nvm which command when in that directory, to enable it.


Dev Better!


Alright, there you have it.  The fundamentals of NVM are covered.  And, quite frankly, that's about it to the system as a whole.  There are some cool features beyond this that might make using NVM easier, but nothing really earth-shattering from here.  Enjoy using NVM, and may your Node versioning issues be left in the past!
Jun 5, 2020 07:44 PM
... mentions are not supported here. Good to know.
Jun 5, 2020 07:43 PM
Thanks [mention=1255039], much appreciated.
May 23, 2020 06:46 AM
Definitely recommend using NVM! Nice article Disease :)