Today was my first day with node.js, and while there seemed to be plenty of tutorials on it's event-based structure and how to write the code, none really seemed to talk about the eco-system. This might be fine for the "Hello World" tutorials, which I was able to complete, but what next? How do I install 3rd party libraries? The Linux distributions have out-of-date packages, right? What happens after "Hello, World!"? The following is what I did to bring myself from having a simple "Hello, World!" without 3rd party libraries to one that uses 3rd party libraries and can be set up on a remote server if necessary. I don't show too much code, I assume you can handle that yourself.
Setting up the Environment for Third-Party Libraries
The first thing to note is that you might have (like me) gotten away with using the node.js that installs with your Linux distribution. You might want to consider uninstalling that, and installing a (most likely) newer version. The best way I've found to do this is to clone the node.js repository, and just install using the install script into a ~/local/bin directory. Keep in mind that if you do things this way, you might want to checkout to a tag of the latest stable version.
git clone git://github.com/joyent/node.git
cd node
git checkout v0.4.11
./configure --prefix=$HOME/local
make install # You may need to install ~/local/bin into your path to get node to run.
As of this writing, the git repository checks you out to somewhere in version 0.5.4, but a more stable version is v0.4.11. You can find the current most stable version on the Node Web Site. There you can also find a download of that version if you don't want to mess with git.
Similar to Ruby Gems and Python Distribute, node.js has npm, or "Node Package Manager". You can install it using a similar method through npm's git repository. Or, you can use the install script which will do everything for you...
curl http://npmjs.org/install.sh | sh
Using npm to install a Library
npm isn't like virtualenv so much as it's like pip, which will install your libraries for you. npm chooses to install your libraries into a directory called "node_modules" in your current directory. For example, to install the mustache.js templating library, you can do...
npm install mustache
You can use the -g option to install the library globally. Useful for something that is more a command-line utility than a library you're using in your code. But of course, this means that you can't install a library that includes some command-line utilities without needing to install it to some directory that ALL your node.js projects will be looking at. There actually is a virtualenv-like library for node.js, but I did not check it out.
Once this was installed, I was able to use the 3rd party library in my code as follows:
var mustache = require('./node_modules/mustache');
npm allows you to define a "package.json" file in the root of your website. While you can see this being useful for creating a library, it's also useful for making a site, since you can define your dependencies. For example...
{
"name" : "mysitename",
"version" : "0.0.1",
"dependencies" : {
"mustache" : "0.3.1-dev"
}
}
Once this package.json file is defined, you can simply run
npm install
to have all of the dependencies installed in your node_modules directory. This will probably be useful later on when you want to deploy your site on a different computer.
Conclusion
At first thought, my unfamiliarity with the node.js ecosystem made me want to just start working without these libraries. Once I understood that you really just need node and npm, and the libraries are installed in a pretty obvious place that doesn't overlap with multiple projects, I felt more comfortable looking into other libraries. One that I'm definitely going to check out is express, which sits on top of node and provides to you many of the elements you might find helpful (routing, redirection helpers, built-in static serving for development, etc.)