Wednesday, 7 December 2016

Node.js - NPM (Node Package Manager)

Node.js - NPM (Node Package Manager)

  1. Online repositories for node.js packages/modules
  2. Command line utility to install Node.js packages, do version management and dependency management of Node.js packages.

To Check Node Version

Open Command line and type
$ npm --version

Installing Modules using NPM

There is a simple syntax to install any Node.js module :

npm install (Module Name)
Example : npm install express

Now you can use this module in your js file as following

var express = require('express');

Uninstalling a Module

Use the following command to uninstall a Node.js module.

npm uninstall express

Once NPM uninstalls the package, you can verify it by looking at the content of /node_modules/ directory or type the following command

npm ls

Updating a Module

Update package.json and change the version of the dependency to be updated and run the following command.

npm update express

Search a Module

npm search express

Global vs Local Installation

  • NPM installs any dependency in the local mode.
  • Here local mode refers to the package installation in node_modules directory lying in the folder.
  • Locally deployed packages are accessible via require() method.
  • npm ls command to list down all the locally installed modules.

  • Globally installed packages/dependencies are stored in system directory.
  • Such dependencies can be used in CLI (Command Line Interface) function of any node.js but cannot be imported using require() in Node application directly.

Now let's try installing the express module using global installation.

npm install express -g

After execute this command line.

express@4.12.2 /usr/lib/node_modules/express
├── merge-descriptors@1.0.0
├── utils-merge@1.0.0
├── cookie-signature@1.0.6
├── methods@1.1.1
├── fresh@0.2.4
├── cookie@0.1.2
├── escape-html@1.0.1
├── range-parser@1.0.2
├── content-type@1.0.1
├── finalhandler@0.3.3
├── vary@1.0.0
├── parseurl@1.3.0
├── content-disposition@0.5.0
├── path-to-regexp@0.1.3
├── depd@1.0.0
├── qs@2.3.3
├── on-finished@2.2.0 (ee-first@1.1.0)
├── etag@1.5.1 (crc@3.2.1)
├── debug@2.1.3 (ms@0.7.0)
├── proxy-addr@1.0.7 (forwarded@0.1.0, ipaddr.js@0.1.9)
├── send@0.12.1 (destroy@1.0.3, ms@0.7.0, mime@1.3.4)
├── serve-static@1.9.2 (send@0.12.2)
├── accepts@1.2.5 (negotiator@0.5.1, mime-types@2.0.10)
└── type-is@1.6.1 (media-typer@0.3.0, mime-types@2.0.10)

You can use the following command to check all the modules installed globally

npm ls -g

Using package.json

package.json is present in the root directory of any Node application/module and is used to define the properties of a package.

Let's open package.json of express package present in node_modules/express/

   "license": "MIT", "repository": {
      "type": "git",
      "url": ""
   "homepage": "", "keywords": [
   "dependencies": {
      "accepts": "~1.2.3",
      "content-disposition": "0.5.0",
      "cookie-signature": "1.0.5",
      "debug": "~2.1.1",
      "depd": "~1.0.0",
      "escape-html": "1.0.1",
      "etag": "~1.5.1",
      "finalhandler": "0.3.3",
      "fresh": "0.2.4",
      "media-typer": "0.3.0",
      "methods": "~1.1.1",
      "on-finished": "~2.2.0",
      "parseurl": "~1.3.0",
      "path-to-regexp": "0.1.3",
      "proxy-addr": "~1.0.6",
      "qs": "2.3.3",
      "range-parser": "~1.0.2",
      "send": "0.11.1",
      "serve-static": "~1.8.1",
      "type-is": "~1.5.6",
      "vary": "~1.0.0",
      "cookie": "0.1.2",
      "merge-descriptors": "0.0.2",
      "utils-merge": "1.0.0"
   "devDependencies": {
      "after": "0.8.1",
      "ejs": "2.1.4",
      "istanbul": "0.3.5",
      "marked": "0.3.3",
      "mocha": "~2.1.0",
      "should": "~4.6.2",
      "supertest": "~0.15.0",
      "hjs": "~0.0.6",
      "body-parser": "~1.11.0",
      "connect-redis": "~2.2.0",
      "cookie-parser": "~1.3.3",
      "express-session": "~1.10.2",
      "jade": "~1.9.1",
      "method-override": "~2.3.1",
      "morgan": "~1.5.1",
      "multiparty": "~4.1.1",
      "vhost": "~3.0.0"
   "engines": {
      "node": ">= 0.10.0"
   "files": [
   "gitHead": "63ab25579bda70b4927a179b580a9c580b6c7ada",
   "bugs": {
      "url": ""
   "_id": "express@4.11.2",
   "_shasum": "8df3d5a9ac848585f00a0777601823faecd3b148",
   "_from": "express@*",
   "_npmVersion": "1.4.28",
   "_npmUser": {
      "name": "dougwilson",
      "email": ""

   "directories": {},
      "_resolved": "",
      "readme": "ERROR: No README data found!"

Attributes of Package.json

name : name of the package
version : version of the package
description : description of the package
homepage : homepage of the package
author : author of the package
contributors : name of the contributors to the package
dependencies : list of dependencies. NPM automatically installs all the dependencies mentioned here in the node_module folder of the package.
repository : repository type and URL of the package
main : entry point of the package
keywords : keywords


Post a Comment