Managing Node.js Versions with nvm

14 December, 2015
post-banner

Managing the version of Node.js you are running for each of your projects is not always the easiest thing to do. With the speed at which new releases of Node are distributed and the vast amount of addons with different dependencies, it's easy to see that switching versions of Node quickly is important. Luckily nvm, the Node Version Manager, is a great tool we can use to easily install and manage different versions of Node.

node-js

A good example of when to use nvm, is to run the brilliant blogging platform Ghost, which I use to publish this blog. It is recommended that you use Node v0.10.x when running Ghost, although v0.12.x and v4.2.x are supported, and this may require you to change your current version.

Installing nvm with Homebrew

My favourite package manager is Homebrew, so I'll install nvm using that. But if you would prefer to use the manual install, then you can check out the GitHub repo for simple instructions on how to do so.

Firstly, it's important to run brew update, and brew upgrade if preferable, before installing nvm, which could take a few minutes.

$ brew update && brew upgrade
$ brew install nvm

Now you should add these lines, in the case of the Homebrew install, to your ~/.bashrc, ~/.profile, or ~/.zshrc file to 'have it automatically sourced upon login'.

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

Once nvm is installed, a ~/.nvm folder is created in your home directory, unless specified otherwise, which will be used to store the installed versions of Node.

Now you should be all set to start managing your Node.js installations.

Installing a New Version of Node

Firstly, you can check which version of Node you are currently using by running node -v or node --version in the terminal.

$ node -v
v4.2.3  

Let's say we want to install the best supported version to be able to run Ghost. NVM will install the specific version set with the latest patch releases, so we can simply install v0.10 to get the latest stable release of 'v0.10'.

$ nvm install 0.10
#################################################################### 100.0%
Now using node v0.10.41 (npm v1.4.29)  

Instead of specifying a specific version, you can install Node with default aliases such as 'stable' and 'unstable' which will install the latest stable and unstable versions respectively.

nvm install stable  
#################################################################### 100.0%
v5.2.0 is already installed.  
Now using node v5.2.0 (npm v3.3.12)  

When you want to switch the version of Node you are running, simply run nvm use <version>.

$ nvm use 0.10

Important note: for each new installation of Node, you will have to globally install your packages. To solve this you can run node rebuild, which will 'recompile all your addons with the new binary'.

Creating Version Aliases

It may not be easy to keep track of which version of Node you need for which particular project, and the simple solution to this is to set a custom alias for specific versions. This is done by using the syntax nvm alias <name> <version>.

For Node v0.10, which we need to run our Ghost project, we can set an alias of 'Ghost' to easily manage this in the future.

$ nvm alias ghost 0.10

Now instead of running nvm use 0.10 every time we want this specific version, we can run nvm use ghost, which is far more memorable.

$ nvm use ghost

Setting the Default Version

Every time you open a new window or session in your terminal, your Node version will be reset. You are able to set a default version which will be used every time you open a new terminal. This is done again by using the syntax nvm alias <name> <version>.

$ nvm alias default stable
default -> stable (-> v5.2.0)  

Here we have set the default version to the release with the alias of stable, which at this current time is Node v5.2.0. Now whenever I open a new terminal window, I can see that I am using the 'stable' version by running nvm current.

$ nvm current
v5.2.0  

Further nvm Commands

If you run nvm help you can see a full list of further nvm commands.

Other useful commands are nvm ls which will list all installed versions and nvm uninstall <versions> which will uninstall the specific version set.

$ nvm ls
v0.10.41  
v4.2.3  
v5.2.0  
default -> stable (-> v5.2.0)  
node -> stable (-> v5.2.0) (default)  
stable -> 5.2 (-> v5.2.0) (default)  
unstable -> 0.11 (-> v0.11.16) (default)  
$ nvm uninstall 0.10.30
Uninstalled node v0.10.30  

As you can see nvm is a very powerful tool for managing Node.js for your web projects. If you are a regular Node.js developer, it's definitely a life saver. If you want to read more, you can check out the GitHub repo.

comments powered by Disqus