DEV Community

Cover image for Managing multiple NodeJS versions
Ido Shamun for daily.dev

Posted on • Originally published at daily.dev

Managing multiple NodeJS versions

We all switch around different projects, sometimes even daily. Every project has its own requirements in terms of dependencies and runtime. Lucky for us, NPM takes care of the dependencies but we still need to manage the runtime. Some projects may use a LTS version and others may live on the edge and use the latest version of node.

Meet NVM

nvm (node version manager) manages multiple node versions and switch between them in an instant.
Even if you use a single node version, it's so much easier to install and update it via nvm.

Installing

Install it using this one-liner:

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

Or check out the full instructions on the GitHub repo

Getting Started

Let's say we want to install node v14.3.0, it's easy as:

nvm install 14.3.0

Just change 14.3.0 to your required version.

If you want to install the latest LTS, run:

nvm install --lts

Once we have a few node versions installed we can activate a specific version with the use command:

nvm use 14.3.0

Global modules

Global modules are not shared across different node versions. You have to install the global dependencies for every node version. It can be annoying but it makes sense. Some dependencies might not be compatible with certain node version.

.nvmrc

Here is the best part! You can add to your project a .nvmrc file to specify exactly the node version.
Going back to our example before, let's save our node version to .nvmrc.

echo "14.3.0" > .nvmrc

Now every time I cd into this directory or its children, I can run nvm use to activate the version of my project. In our case it's 14.3.0.

I can even commit this file to the git repo so other developers can use it as well.

That's it! Now you can easily switch between projects without thinking about the desired node version. πŸ‘Ύ


Daily delivers the best programming news every new tab. We will rank hundreds of qualified sources for you so that you can hack the future.

Daily Poster

Top comments (11)

Collapse
 
stereoplegic profile image
Mike Bybee • Edited

A few extra pointers:

  • Homebrew for Mac or Linux is the easiest way to install NVM (brew install nvm), and it's one less separate update to manage (just update it with the rest of your brew packages).
  • Contrary to popular belief, it's entirely possible to use NVM as well as a "system" version of Node installed elsewhere (e.g. via brew install node@[version] on Mac and Linux, or from your Linux distro's repository, or from the official NodeSource deb or rpm repo). For everyday use, set NVM to use this version with nvm use system. The benefit of this is not having to redo your global node_modules for every new point release (and not needing to worry about either uninstalling previous ones or having several stale node_modules folders), as well as not needing to install yarn (e.g. via Homebrew) only to follow up by removing the Node it pulls down as a dependency. From there, when you need a specific NVM-provided version of Node, just do nvm use [version] or, even better, specify it in your project with a .nvmrc file.
Collapse
 
joelrozen profile image
Joel Rozen

Check out asdf version manager.
A lot more fiddly but oh-so-much more powerful.

Collapse
 
ojoanalogo profile image
Alfonso Reyes

On other alternatives to nvm I found Volta (github.com/volta-cli/volta) I haven't tried it personally but seems like a good alt

Collapse
 
idoshamun profile image
Ido Shamun

I love the simplicity of NVM. But never heard of asdf

Collapse
 
mjgs profile image
Mark Smith

Nvm is for sure a cool tool, the version switching functionality is great.

I’ve skimmed through docs but couldn’t find this - Does nvm have a feature to install and compile node from source?

Collapse
 
idoshamun profile image
Ido Shamun

I'm not familiar with such feature. Why would you need it?

Collapse
 
mjgs profile image
Mark Smith

Compiling from source (as opposed to using pre-compiled binaries) is considered by some to be more secure.

Collapse
 
anzhari profile image
Anzhari Purnomo

Great article!

I personally use nvm and it is really helpful between switching the latest and LTS version.

Collapse
 
idoshamun profile image
Ido Shamun

Thanks. Yeah, it's super cool!

Collapse
 
budimirbudimir profile image
Budimir

One of my fav tools ever is tj/n. N is so much easier and more reliable than NVM. It is also somewhat more intelligent. Only downside is it's OSX-only tool.

Collapse
 
kasiriveni profile image
Srinivas Kasiriveni

πŸ‘πŸ‘