Using nvm to manage node versions

27 April, 2016

Usually we only have one machine, but we want to use different node to support different apps, like if we want to run Ghost, the recommended node version is 0.10.x, but this version is really old, our new project may use 4.x or higher, but how do we have the best of the both world?

The answer is that we can use nvm to install multiple node versions for different projects.

Go to the nvm github page , use the script to install nvm:

curl -o- | bash

or other alternative scripts.
Then you can nvm --version to see the version:

nvm version

And you are about to go: nvm install 4 to install the latest node 4.x version,
nvm ls to see all the node versions installed.

nvm ls

You can also install iojs if you want.

Sometimes you don't want to type the version everytime you use a different version, for this, you can set the alias for different node versions:
nvm alias default 4
nvm alias newNode 6,
after that you can just
nvm use newNode to switch among defferent versions.

You can also run node in different node version directly from the single line command: nvm exec 6 node app.js run app.js in version 6.x.

How it works

After you install nvm, there will be a .nvm dir in your home dir, all the versions will be install inside this, which will be pointed to node according the version you are currently using:

which node
# ~/.nvm/versions/node/v4.4.0/bin/node

the current node will be added to your PATH env before the default one /usr/local/bin/node:
echo ${PATH}
and the node in nvm will have higher priority.

Now you can use any node version as you wish without deleting the old one and installing the new one, which is really a headache:(.


Another node version manager you can try: n