14 Dec

Taking Baby Steps with Node.js – Using Non-Standard Modules

Category:UncategorizedTag: :

In previous blog posts, I provided a short introduction to Node.js while also discussing the event-based model that lies at its core. For this blog post, I want to show how to effectively use non-standard JavaScript modules from a Node.js application.

As already discussed in the introductory blog post, Node.js comes with a number of built-in low-level modules. But the increasing amount of open-source modules out there makes developing applications for Node.js way more productive and effective. So far I?ve been using a package manager called Npm for quickly installing these open-source modules on my machine. By default, Npm installs all packages in the local folder of Node.js (usr\local\ lib\node\).

$ npm install express

This installs the necessary packages for the express web development library.








In our Node.js application we can now simply put the following require statement and start using this installed module.

var express = require(?express?); 

application.get('/', function(request, response){ 
    // Some code


When another developer wants to run the application by getting the source files from source control, he first has to make his way through the code to determine all non-standard modules that have been used and install the correct version of these modules. When developing .NET applications, it?s generally a best practice to put all third-party libraries in a dedicated library folder alongside your code in source control. This enables the code to be compiled and run with the same libraries that were used during development. Of course this same principle applies to server-side JavaScript as well.

The simplest way to accomplish this is to create a library folder alongside the folder that contains the source code and use git to get the latest version of a particular module.

$ git clone http://github.com/visionmedia/express.git /MyProj/lib/express

Next we add our library folder to the paths that are used by Node.js for looking up the required modules.

require.paths.unshift(__dirname + '/lib');
var express = require(?express?); 

application.get('/', function(request, response){ 
    // Some code


We just add the special variable __dirname together with the name of our library folder in the main source file (usually named something like server.js). Now whenever a require statement is used, Node.js will first look for the requested module in our custom library folder before looking into other configured directories.

You can still use npm instead of git for retrieving and installing packages into a custom folder, but then you have to create an ini file named .nmprc in your home folder:

cat >>~/.npmrc <<NPMRC
root = /<full_path_my_project>/lib
binroot = ~/bin
manroot = ~/share/man

There you go. I hope this might be useful for someone someday. I?m definitely having loads of fun learning about Node.js, taking one step at a time.

Until next time.