How to Install Package Dependencies in Node.js Application using npm

Posted on

The main information given in this article is primary about how to install package dependencies in Node.js application using npm. The Node.js application mentioned in this article, in the case of this article is a Web-based application powered by express framework. The focus of creating the web-based application powered by express framework has been shown in the article titled ‘How to Create an Express Web-based Framework Application to be deployed in Node.js’ in this link.

But before the web-based application using express framework can be executed in the Node.js platform, there are modules or packages which is specified in the file package.json located inside the root of the folder created or represented as the web-based application, it must be installed first. The installation process itself can be done without having to do it manually but by executing the following command :

npm install

The npm (node package manager) will read the package.json file looking for the dependencies section as shown below :

{
  "name": "application-name",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "nodejs ./bin/www"
  },
  "dependencies": {
    "express": "~4.0.0",
    "static-favicon": "~1.0.0",
    "morgan": "~1.0.0",
    "cookie-parser": "~1.0.1",
    "body-parser": "~1.0.0",
    "debug": "~0.7.4",
    "jade": "~1.3.0"
  }
}

In this case, npm will download all of the packages or modules specified in the dependencies section and will store or save those packages or modules in a location or a folder inside the root folder of the web-based application associated. The folder name normally is created with the name of ‘node_modules’. Before the command itself is being executed, the content of the web-based application folder doesn’t even have any folder named ‘node_modules’ as shown below :

user@hostname:~/nodejs/test$ ls
app.js  bin  package.json  public  routes  views
user@hostname:~/nodejs/test$

Below is the output process of the ‘npm install’ command generated :

user@hostname:~/nodejs/test$ npm install
npm WARN deprecated jade@1.3.1: Jade has been renamed to pug, please install the latest version of pug instead of jade
npm WARN deprecated static-favicon@1.0.2: use serve-favicon module
npm WARN deprecated transformers@2.1.0: Deprecated, use jstransformer
npm WARN deprecated constantinople@2.0.1: Please update to at least constantinople 3.1.1
application-name@0.0.1 /home/hamdi/nodejs/test
├─┬ body-parser@1.0.2 
│ ├── qs@0.6.6 
│ ├─┬ raw-body@1.1.7 
│ │ ├── bytes@1.0.0 
│ │ └── string_decoder@0.10.31 
│ └─┬ type-is@1.1.0 
│   └── mime@1.2.11 
├─┬ cookie-parser@1.0.1 
│ ├── cookie@0.1.0 
│ └── cookie-signature@1.0.3 
├── debug@0.7.4 
├─┬ express@4.0.0 
│ ├─┬ accepts@1.0.0 
│ │ └── negotiator@0.3.0 
│ ├── buffer-crc32@0.2.1 
│ ├── escape-html@1.0.1 
│ ├── fresh@0.2.2 
│ ├── merge-descriptors@0.0.2 
│ ├── methods@0.1.0 
│ ├── parseurl@1.0.1 
│ ├── path-to-regexp@0.1.2 
│ ├── range-parser@1.0.0 
│ ├── send@0.2.0 
│ ├─┬ serve-static@1.0.1 
│ │ └─┬ send@0.1.4 
│ │   ├── fresh@0.2.0 
│ │   └── range-parser@0.0.4 
│ ├── type-is@1.0.0 
│ └── utils-merge@1.0.0 
├─┬ jade@1.3.1 
│ ├── character-parser@1.2.0 
│ ├── commander@2.1.0 
│ ├─┬ constantinople@2.0.1 
│ │ └─┬ uglify-js@2.4.24 
│ │   ├── async@0.2.10 
│ │   ├─┬ source-map@0.1.34 
│ │   │ └── amdefine@1.0.1 
│ │   ├── uglify-to-browserify@1.0.2 
│ │   └─┬ yargs@3.5.4 
│ │     ├── camelcase@1.2.1 
│ │     ├── decamelize@1.2.0 
│ │     ├── window-size@0.1.0 
│ │     └── wordwrap@0.0.2 
│ ├── mkdirp@0.3.5 
│ ├─┬ monocle@1.1.51 
│ │ └─┬ readdirp@0.2.5 
│ │   └─┬ minimatch@3.0.4 
│ │     └─┬ brace-expansion@1.1.11 
│ │       ├── balanced-match@1.0.0 
│ │       └── concat-map@0.0.1 
│ ├─┬ transformers@2.1.0 
│ │ ├─┬ css@1.0.8 
│ │ │ ├── css-parse@1.0.4 
│ │ │ └── css-stringify@1.0.5 
│ │ ├─┬ promise@2.0.0 
│ │ │ └── is-promise@1.0.1 
│ │ └─┬ uglify-js@2.2.5 
│ │   └── optimist@0.3.7 
│ └── with@3.0.1 
├─┬ morgan@1.0.1 
│ └── bytes@0.3.0 
└── static-favicon@1.0.2 

user@hostname:~/nodejs/test$ 

After the command execution of ‘npm install’ precisely in the root folder of the web-based applicationi as the current working directory, the folder named ‘node_modules’ is created with all of the packages or modules downloaded and stored inside of it. The following output shown all of it :

user@hostname:~/nodejs/test$ ls
app.js  bin  node_modules  package.json  public  routes  views
user@hostname:~/nodejs/test$ cd node_modules/
user@hostname:~/nodejs/test/node_modules$ ls
accepts          buffer-crc32      constantinople    css-stringify  is-promise         mkdirp      path-to-regexp  send            type-is               wordwrap
amdefine         bytes             cookie            debug          jade               monocle     promise         serve-static    uglify-js             yargs
async            camelcase         cookie-parser     decamelize     merge-descriptors  morgan      qs              source-map      uglify-to-browserify
balanced-match   character-parser  cookie-signature  escape-html    methods            negotiator  range-parser    static-favicon  utils-merge
body-parser      commander         css               express        mime               optimist    raw-body        string_decoder  window-size
brace-expansion  concat-map        css-parse         fresh          minimatch          parseurl    readdirp        transformers    with
user@hostname:~/nodejs/test/node_modules$

Leave a Reply