Mar 23

Installing and switching between multiple versions of Node.js: n vs. nvm


The need for a Node Version Manager

Sometimes it is necessary to be able to quickly switch between versions of Node.js without having to uninstall and reinstall a new version. Perhaps one project is written for an older version of Node.js and works on that version, but you’d like to easily test it against a newer version of Node, or perhaps other projects you manage require a newer version of node and you need to be able to switch back and forth. There are two very good Node version management tools for this purpose: n and nvm, they work somewhat differently and each tool has its’ own advantages and disadvantages. I’ll be discussing these differences based on my experience using them on the Mac OS X platform, there will be minor differences in the paths on a linux distro like Ubuntu and for the Windows platform you’ll want to use other packages (nvmw or Nodist).

n at a glance

GitHub: https://github.com/visionmedia/n
Install: $ npm install -g n
Path to Script: /usr/local/bin/n
Node Versions Path: /usr/local/n/versions
How n gets a new version: Downloads Binary Node tar.gz file for platform
How n switches versions: Copies files from /usr/local/n/versions/ to /usr/local/

nvm at a glance

GitHub: https://github.com/creationix/nvm
Install: curl https://raw.github.com/creationix/nvm/master/install.sh | sh
Path to Script: ~/.nvm/nvm.sh
Node Versions Path: ~/.nvm/
How nvm gets a new version: Downloads and compiles source node distribution (binary option also available)
How nvm switches versions: updates $PATH variables to point to requested version within ~/.nvm/ directory

n vs. nvm

Although both n and nvm achieve the same goal (allows installing and switching between node versions), n will move your node files always to the same path (/usr/local/bin/node), so you’ll always know that your current executable lives there. One of the downsides of n is that is will only download and install binary node distributions, for older releases (pre-v0.8.6) you’ll have to compile and install the version on your own and then manually copy the files into your /usr/local/n/versions/ directory in order to switch to those versions. On the other hand, since nvm downloads, compiles and installs from the source files, it will work for just about any node.js version. One downside is that since nvm simply manipulates the $PATH variable to point to the requested version some programs and IDEs (such as WebStorm) that need to point to a full path will have to use the full ~/.nvm//bin/node path.

Can I use both n and nvm?

Sure, but you might not want to use both at the same time since they will conflict. If you want to disable nvm just comment out the line that sources the script file from your .profile or .bashrc file:

[[ -s ~/.nvm/nvm.sh" ]] && . ~/.nvm/nvm.sh

and then run:

source .profile

to remove the nvm script from your path.

If you need to run node versions older than v0.8.6, I’ve found that it’s convenient to install them using nvm, then after it’s installed simply copy the folder over from the nvm directory to the n path:

nvm install 0.6.21
sudo cp -fr ~/.nvm/v0.6.21 /usr/local/n/versions/0.6.21

This allows you to use these versions going forward in n.

  • Chris

    Nice explanation and comparison. Thanks! šŸ™‚

  • Harish

    Thanks for the details

  • Louis

    To be able to grab the nvm install.sh script directly, the path has updated:

    curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh
    wget https://raw.githubusercontent.com/creationix/nvm/master/install.sh

    Thanks for the great article. It’s a bit maddening to only be able to run one version of node for multiple apps at a time, so I was happy to find out about nvm and it’s support for relative install points. Cheers!

    • Louis

      And the full paths got truncated. šŸ™

  • George Norris

    I’ve run into a bunch of problems with nvm and Sublime Text linter / build system. Switched to n and things are much easier..

  • I just confused of that!

  • broberson

    FYI, if you want to use different versions of node simultaneously (like, in two different terminal windows at the same time), nvm is the way to go, since it’s just manipulating your path. It basically sets the node version for your current session, rather than system-wide. Juggling multiple projects, this has been a real timesaver for me.

    • derOlli

      With n, it’s also possible to use different versions in multiple terminal windows:

      n use 0.9.4 some.js