When I first heard about nvm (a command line tool that allows you to easily switch between different versions of Node) I thought "that's probably one of those things that is really useful, but a pain in the butt to install and configure".

I was right about it being really useful, but it wasn't that much of a pain in the butt to get working.

I started by running

npm install -g nvm  

It installed ok, but I got a message telling me that I shouldn't use npm to install nvm, but instead to go to http://nvm.sh, which redirects to https://github.com/creationix/nvm/blob/master/README.markdown.

That page directed me to use curl to download the installer and pipe it directly to bash, like so:

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.0/install.sh | bash  

I ran

npm uninstall -g nvm  

and then ran that curl one-liner.

When it finished I got the following message:

=> Close and reopen your terminal to start using nvm or run the following to use it now:

export NVM_DIR="/Users/stevensmith/.nvm"  
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm

I copied and pasted those commands in my terminal, hit enter, and nvm was installed.

Once you have nvm installed, you can install a different versions of Node like so:

nvm install 4.6.0  

Once you install, nvm switches you to that version. If you want to see all the versions you've got installed at the moment, just run:

nvm ls  

Here's what I get when I run nvm ls:

->       v4.6.0
default -> 6.3.0 (-> v6.3.0)  
node -> stable (-> v6.7.0) (default)  
stable -> 6.7 (-> v6.7.0) (default)  
iojs -> N/A (default)  
lts/* -> lts/argon (-> v4.6.0)  
lts/argon -> v4.6.0  

Most of this makes sense to me. I've got 5 versions of Node installed at the moment: 4.4.7, 4.6.0, 5.12.0, 6.3.0, and 6.7.0.

The little arrow is pointing at version 4.6.0, which is the version I currently have active. If I wanted to switch to 6.7.0 I'd just have to run

nvm use 6.7.0  

Pretty simple right? Now you can make sure your code will run on the version your target environment is running. For example, if the web server for your project has a different version than what you usually develop in.

Looking for a software developer?