Blog

  • Home /
  • Blog /
  • Taking Baby Steps with Node.js – Using Non-Standard Modules

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

December 14, 2010

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. 

image

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

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.

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:

var express = require(‘express’); 

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

application.listen(8124);

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.

Profile picture of Jan Van Ryswyck

Jan Van Ryswyck

Thank you for visiting my blog. I’m a professional software developer since Y2K. A blogger since Y2K+5. Curator of the Awesome Talks list. Past organizer of the European Virtual ALT.NET meetings. Thinking and learning about all kinds of technologies since forever.

Comments

About

Thank you for visiting my website. I’m a professional software developer since Y2K. A blogger since Y2K+5. Curator of the Awesome Talks list. Past organizer of the European Virtual ALT.NET meetings. Thinking and learning about all kinds of technologies since forever.

Contact information

(+32) 496 38 00 82

infonull@nullprincipal-itnull.be