Managing Multiple Versions of Node

With the amount of progress in Node, at some point, you will encounter version problems. For example, I recently encountered a problem with my local Ghost installation because I’m using the latest version of Node. Ghost, being a large Node application, hasn’t yet migrated to the latest version and is still using a very old version – v0.10.40.

Luckily, this can be solved by using nvm. NVM simply means Node Version Manager and is similar to rvm and rbenv in Ruby. It handles the Node versions for you. Once installed, it creates a ~/.nvm folder in your home directory and it will manage Node in that directory.

Installation

To install nvm, just follow the instructions at the official Github page. Depending on your operating system, there will be slight variations in the installation process.

I’m using OS X and zsh as my shell, so I just typed the following commands:

brew update  
brew install nvm  

After installation, I also had to add the path of NVM for it to be recognized in my terminal. I added the following lines to my .zshrc file.

export NVM_DIR=~/.nvm  
. $(brew --prefix nvm)/nvm.sh

The dot in the second line above is required.

Using NVM

NVM allows you to easily switch to different Node versions without messing up your system. To install a different version of Node, just use the install command:

nvm install 4.2  
nvm install 5.5  

It will automatically switch to the newly installed version after installation. You can use the following command to switch to your desired Node version. For example, if I want to switch to an older version, say v.10.40, I would simply type:

nvm use 0.10.40

To verify which Node version you are currently using, use the which command:

which node

/*
/Users/karlo/.nvm/v0.10.40/bin/node
*/

You can also check which Node versions you have already installed so far by using the ls command

nvm ls

/*
        v0.10.40
        v4.2.0
->      v5.5.6
        system
*/

To view the available versions of Node that you can install, use ls-remote command:

nvm ls-remote

When you no longer use a specific version of Node, you can also remove it using the uninstall command

nvm uninstall 4.2.0

NVM is one of the indispensable tools for me when working on Node projects. It’s a very useful tool when working on multiple Node applications that are using different versions of Node.

Additional Notes

When you are installing a global package on a system managed by nvm, it will install the package to the ~/.nvm/[version]/bin directory. This means that when you install a new global package, you have to keep in mind that the global package installed using npm install -g command will be installed in the directory of the current Node version that is active.