Node and Its Many Incarnations (Node Version Management)
[tweetmeme source=”anismiles” only_single=false http://www.URL.com%5D
Node.js is under active development. And every other day, a new build is released. It’s awesome to see how fast Node is growing and how vibrant the community is… but on the down side, it’s becoming increasingly difficult to keep track of its many versions, and API changes.
Very often, while developing an app, you find yourself married into a particular Node version, because a newer one might have some API changes (mind you, Node is witnessing heavy transformations, especially at the API level) which might break you app… and then, you would be forced to revert back to the older version. That means, uninstall the current node and re-install the older one. Ouch! So much work for a mere upgrade.
Well, there is a nicer way to do it. Check out this project by Tim Caswell: Node Version Manager. It does exactly what it says. It manages various Node versions on your machine, development, stage, production whatever. How?
It creates a virtual Node environment for each version you want to keep. Let’s say, you want to stay with the last stable release v0.2.6 (from the time you started your app) but also want to experiment with v0.4.7 to keep an eye on new additions. NVM will install two separate Node(s) for you, and each will run in its own sandbox like environment, that is, you will have to install all your third party Modules/Libraries separately for each Node installation. That might seem to be a lot of work, but trust me, it’s the safest way to avoid conflicts. Okay. Let’s get to work.
Note: I am assuming that you have basic knowhow of GIT (the most awesome source control management system).
- Clone NVM repository to your local machine:
$ git clone git://github.com/creationix/nvm.git ~/.nvm
Above command would close the NVM repository to a folder ‘.nvm’ in your home directory. (I am using Ubuntu 10.0.4)
- Switch to folder ‘.nvm’ and make file ‘nvm.sh’ executable:
$ chmod 755 ~/.nvm/nvm.sh
- ‘nvm.sh’ is just a shell script, so in order to run it, you must source it to every terminal you open. To do this automatically, simply edit either ‘.bashrc’ or ‘.profile’ file to have this line in the very end:
- That’s it. Open a new terminal and run,
- You will see a set of useful commands you can use. 🙂 Easy huh?
Before you get any further, just make sure that you have ‘wget’ installed in your machine. I know, I know… you might already have it. I just want you to make sure.
Check which versions of Node are available.
$ nvm sync // update the local machine with available versions from server $ nvm ls // displays all available and installed versions
Now install Node v0.4.7.
$ nvm install v0.4.7 // will install Node v.0.4.7
Note: You might get this error, “Could not autodetect OpenSSL support. Make sure OpenSSL development packages are installed. Use configure –without-ssl to disable this message” which says, that you need to install SSl library:
$ sudo apt-get install libssl-dev
NVM creates a folder ‘src’ either in your home directory or in ‘.nvm’ directory where it downloads the bundled release, extracts and installs it. NVM also installs NPM (node package manager) for each installation of Node.
Select a particular version
$ nvm use v0.4.7 // start using Node-v0.4.7
That’s it. You have set up a system which will enable you to quickly and cleanly switch between various Node versions. You can test your app’s compatibility with any of them, and if need be, easily switch to the one your app was most comfortable with.
Now, since you have set up a congenial Node development machine, in the next blog, I will talk about how to go live with your Node app.
Note: for CentOs-5.x, please make sure that you have following packages installed:
$sudo yum install gcc-c++ screen git-core openssl openssl-devel