How I manage multiple Node.js versions

Node.js is a lightweight Javascript runtime. It follows the Semantic Versioning scheme so every major release has multiple API breaking changes. We change our Node.js version to make a certain app work or to upgrade to the latest version.

After evaluating different popular options including NVM, Ubuntu Repos, Ubuntu PPAs, OSX Homebrew and OSX Macports I experienced a lot of issues with them including

  1. They are OS specific
  2. You cannot install specific Node.js versions
  3. They can cause conflicts when more than one users try to run different versions of same global modules
  4. You have to use sudo when installing global modules or linking local ones (except for homebrew)

Having to do sudo for npm is bad for a lot of reasons, for examples the directories/files it creates while running as sudo will be owned by root and you won’t get to modify them afterwords.

How I do it

I use the n Node.js version manager by TJ Holowaychuk. Here’s it’s pros and cons in comparison

Things awesome about n

  1. Zero overhead (unlike NVM)
  2. Works on any POSIX environment
  3. Never asks for or requires sudo
  4. Can download any Node.js version
  5. Installs modules in ~/n (configurable) so they are isolated per user

Possible downsides of n

  1. Too easy if you are a real programmer 😁

How you can too

You should uninstall the currently installed Node.js before anything else and then execute these in order

curl -L https://git.io/n-install | bash
# Press y in the prompt or configure the directory if you want
# Then restart your shell to update it's $PATH and be able to use n/node
exec $SHELL

That’s it fellaws, now you have an isolated, working Node.js setup. If you went with the defaults you should have an n directory in your home directory.

Basic Usage

n is a bash script so it’s lightweight/minimal but still includes all the necessary features.

To upgrade to the latest version of Node.js do

n latest

or to install the lts version do

n lts

or say you want to download Node.js v8.9.4 do

n 4.4.1

If you want to access the list of installed versions and select between them, do

n

You can find more about n in it’s README.

Note: When switching between shells, remember to also include the line inserted by n-install in your shell’s config file such as ~/.bash_profile, ~/.bashrc or ~/.zshrc.

That’s all for now folks, happy coding!