Node.js Version Control: Streamlining with nvm

Node.js Version Control: Streamlining with nvm

Working with NodeJS is a lot of fun, managing versions not so much. If you have ever done any serious Node development you have probably ran into some version problems at some point. Luckily that is a problem that is easily solved with nvm.

What is nvm?

nvm stands for Node Version Manager. It's really just a bash script that manages your Node versions for you. When you install it, it will create a ~/.nvm folder in your home directory (unless you specify another directory of course) and it will manage your versions in there.

Installing nvm

You can install nvm the "easy way" by just running:

curl -o- | bash

Warning: NEVER, really NEVER run a bash script from a remote server without checking it out first. A lot of harm can be done to your machine.

A safer way is to install nvm manually:

git clone ~/.nvm && cd ~/.nvm && git checkout `git describe --abbrev=0 --tags`

. ~/.nvm/

and finally add the following to your ~/.zshrc or ~/.bashrc:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/" ] && . "$NVM_DIR/"

The install script will do all that for you but it's generally a bad idea to just run bash scripts if you haven't read the source.

Installing a new Node version

Now when you have nvm up and running it's time to add a new version and to create aliases. My favourite blogging platform Ghost recommends that you use the latest Node v0.10.x release.

nvm will install the version you specify including all the patch releases. So running nvm install 0.10 will install v0.10.40 but nvm install 0.10.23 would install v0.10.23.

nvm will also switch to your newly installed version after installation. It's that easy.

Creating aliases

After a while you are probably going to forget what Node version every project you work with uses. That's why you have aliases. Creating an alias is done with the following syntax: nvm alias [name] [version].

I have created an alias for Ghost by running nvm alias ghost 0.10. Now rather than running nvm use 0.10 I can just run nvm use ghost.

To remove an alias run nvm unalias ghost. This will only remove the alias of course, not the Node version.

Global package installation

When you install a Node package globally on a nvm managed version, it will install it to the ~/.nvm/[version]/bin directory. This is great because I might want to use the process manager pm2 while running Ghost without installing it on my system's Node instance.


Now you're all up and running with nvm. I recommend you check out the project on GitHub. If you enjoyed this post or have feedback please leave a comment below or send me a tweet @Jilles.