What is NPM (Node Package Manager
Jul 8, 2012
Suresh Vasudev
4 minute read

Node Package Manager

Modules are library of codes which can be reused in multiple projects. E.g.: Database Driver can be treated as a module which can be reused in multiple node.js applications. Modules in Node.js is similar to RubyGems in Ruby

Most of the node.js modules are open source which means they are readily available to use or modify and redistribute

The installer for node.js available at http://nodejs.org contains npm. In UNIX it is advised not to install it using sudo permission.

To make sure npm is installed type “npm” in a command / terminal window.


Installing modules using npm

To install a module use the command “npm install [module name]

Modules are often need to be installed at project level and so one needs to go to the project directory to install the module.

The command will send a request to the npm registry server to download the latest version of the module. Once the module is downloaded it will print the module name, its version and the location where it is downloaded.

The computer MUST be connected to the internet to install module as it needs to access the npm registry in Internet.

How to use a module which is downloaded

It MUST be used just like any other modules available in node.js. We shouild use the require(‘[moduel name]’) method to get access to the module instance.

How to find available node.js modules

There are official and unofficial location to store the modules. The official location is http://search.npmjs.org/ which will allow to search the official npm registry

There is also a provision to search from the command / terminal window using “npm search [keywords separated by space]”

Some of the unofficial sites to search for modules are http://blago.dachev.com/modules http://eirikb.github.com/nipster/ http://toolbox.no.de/

Module installation Approach

Local Installation

This is the default installation and it is for a specific project. The command is npm install [module name] It will create a folder “node_modules” in the project root folder and will create subfolder in the name of module insider “node_modules” to store the module.

This is the most common and recommended way of installing node.js modules.

Global Installation

It allows to install modules globally so that it can be executed from anywhere within the system. The only difference in command is -g option. npm install -g [module name]

Most of the time modules that aide development is installed globally. Example is Express module, which is a web development framework for node.js

Module Documentation

To use a module it needs to be installed first. Once installed we need to know the APIs of this module. It is available using the npm docs [module name] command.

To find the known bugs of a module use the command npm bugs [module name]

To read the source code of a modules use npm edit [module name]

Managing dependencies using package.json

A project will be depending on multiple third party modules and will be version specific. It is difficult to remember the different compatible versions of each module required for a project and download it in various servers

There is a provision in node.js project to define a package.json file at root level and define all the module dependencies. It is like Gemfile in ROR.

Once the package.json is defined all modules defined in that can be installed in one shot using npm install command. This will help to develop application as a team and deploy it in multiple servers with ease.

Deploying a module manually for a project

  1. Create a project example01
  2. cd example 01
  3. npm install Accessor_MySQL
  4. check whether module got installed.


Check whether the module got installed. Use npm list to get the list of all the modules installed.


Deploying module using package.json

[1] Create a new project directory - example02 [2] cd to example02 [3] create a package.json file which looks for 3 modules

Accessor_MongoDB Accessor_MySQL Accessor_Singleton


{ “name”: “example02”, “version”: “1.0.0”, “dependencies”: { “Accessor_MongoDB”: “0.2.0”, “Accessor_MySQL”: “0.2.0”, “Accessor_Singleton”: “0.2.0” } } [/javascript]

[4] save the package.json file into project root directory [5] npm install [6] npm list


In this example npm install uses the package.json and deployed all the modules defined in it. it is always advisable to create package.json to install modules.