6 Jan 2014

Introduction to npm (NodeJS package manager) - NodeJS

1/06/2014

Like any other programming language which has a way to create a package, NodeJS package manager most often called as npm is how dependencies can be resolved while creating a NodeJS application. You can compare it to a maven packaging that we do in a java application.

The basic file that is at the core required to create a package is package.json

Step 1 : Create a package.json 

At the application create a file called package.json, lets see a sample package.json file
{
  "name": "RestApp",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node app.js"
  },
  "dependencies": {
    "express": "3.2.6",
    "jade": "*",
    "mongoskin": "~0.6.0"
  }
}

Now in this file you can easily drive similarity with a pom.xml file that you have in maven project.

The above example shows the simplest form and a set of standard stuffs that you declare in your NodeJS application.

The first section is more of a identifier for the application where you declare the name, version, private and starting js file which is the key file to load the application. Next comes the dependencies part, NodeJS as such has numerous number of modules (can be treated as different frameworks developed and ready to be used to solve a specific problem statement) which can be imported in your NodeJS application and used. The way to do it is declaring a json field in your dependencies of your package with the name and the version of the module that you want to use. 
In this example there are three modules that has been added in the package as a dependency, we will try to cover even some of the popular modules later in this section briefly.

Step 2 : Install a package / dependencies in a NodeJS application

Now once you have created your package.json for your application lets try to understand how to install the package and what actually it does.

Lets create a home directory for my application, say i would use D:/RestApp to be my app home location

I will go to the command prompt of my application home location and execute the following command

npm install

which resulted in to the below trace on my command prompt

D:\RestApp>npm install
npm http GET https://registry.npmjs.org/express/3.2.6
npm http GET https://registry.npmjs.org/mongoskin
npm http GET https://registry.npmjs.org/jade
npm http 304 https://registry.npmjs.org/jade
npm http 304 https://registry.npmjs.org/mongoskin
npm http 304 https://registry.npmjs.org/express/3.2.6
npm http GET https://registry.npmjs.org/connect/2.7.11
npm http GET https://registry.npmjs.org/range-parser/0.0.4
npm http GET https://registry.npmjs.org/cookie/0.1.0
npm http GET https://registry.npmjs.org/fresh/0.1.0
npm http GET https://registry.npmjs.org/buffer-crc32/0.2.1
npm http GET https://registry.npmjs.org/mkdirp/0.3.4
npm http GET https://registry.npmjs.org/methods/0.0.1
npm http GET https://registry.npmjs.org/commander/0.6.1
npm http GET https://registry.npmjs.org/cookie-signature/1.0.1
npm http GET https://registry.npmjs.org/send/0.1.0
npm http GET https://registry.npmjs.org/debug
npm http GET https://registry.npmjs.org/mongodb/1.3.19
npm http GET https://registry.npmjs.org/mkdirp
npm http GET https://registry.npmjs.org/commander/2.0.0
npm http GET https://registry.npmjs.org/character-parser/1.2.0
npm http GET https://registry.npmjs.org/monocle/1.1.50
npm http GET https://registry.npmjs.org/with
npm http GET https://registry.npmjs.org/transformers/2.1.0
npm http GET https://registry.npmjs.org/constantinople
npm http 304 https://registry.npmjs.org/range-parser/0.0.4
npm http 304 https://registry.npmjs.org/connect/2.7.11
npm http 304 https://registry.npmjs.org/buffer-crc32/0.2.1
npm http 304 https://registry.npmjs.org/cookie/0.1.0
npm http 304 https://registry.npmjs.org/fresh/0.1.0
npm http 304 https://registry.npmjs.org/mkdirp/0.3.4
npm http 304 https://registry.npmjs.org/methods/0.0.1
npm http 304 https://registry.npmjs.org/commander/0.6.1
npm http 304 https://registry.npmjs.org/send/0.1.0
npm http 304 https://registry.npmjs.org/cookie-signature/1.0.1
npm http 304 https://registry.npmjs.org/debug
npm http 304 https://registry.npmjs.org/mongodb/1.3.19
npm http 304 https://registry.npmjs.org/mkdirp
npm http 304 https://registry.npmjs.org/commander/2.0.0
npm http 304 https://registry.npmjs.org/character-parser/1.2.0
npm http GET https://registry.npmjs.org/mime/1.2.6
npm http GET https://registry.npmjs.org/bson/0.2.2
npm http GET https://registry.npmjs.org/kerberos/0.0.3
npm http GET https://registry.npmjs.org/qs/0.6.5
npm http GET https://registry.npmjs.org/cookie/0.0.5
npm http GET https://registry.npmjs.org/bytes/0.2.0
npm http GET https://registry.npmjs.org/formidable/1.0.14
npm http GET https://registry.npmjs.org/send/0.1.1
npm http GET https://registry.npmjs.org/pause/0.0.1
npm http 304 https://registry.npmjs.org/monocle/1.1.50
npm http 304 https://registry.npmjs.org/with
npm http 304 https://registry.npmjs.org/transformers/2.1.0
npm http 304 https://registry.npmjs.org/constantinople
npm http GET https://registry.npmjs.org/uglify-js/2.4.0
npm http GET https://registry.npmjs.org/promise
npm http GET https://registry.npmjs.org/css
npm http GET https://registry.npmjs.org/uglify-js
npm http GET https://registry.npmjs.org/uglify-js
npm http GET https://registry.npmjs.org/readdirp
npm http 304 https://registry.npmjs.org/bson/0.2.2
npm http 304 https://registry.npmjs.org/kerberos/0.0.3
npm http 304 https://registry.npmjs.org/qs/0.6.5
npm http 304 https://registry.npmjs.org/cookie/0.0.5

> kerberos@0.0.3 install D:\RestApp\node_modules\mongoskin\node_modules\mongodb\
node_modules\kerberos
> (node-gyp rebuild 2> builderror.log) || (exit 0)


D:\RestApp\node_modules\mongoskin\node_modules\mongodb\node_modules\kerberos>nod
e "C:\Program Files\nodejs\node_modules\npm\bin\node-gyp-bin\\..\..\node_modules
\node-gyp\bin\node-gyp.js" rebuild

> bson@0.2.2 install D:\RestApp\node_modules\mongoskin\node_modules\mongodb\node
_modules\bson
> (node-gyp rebuild 2> builderror.log) || (exit 0)


D:\RestApp\node_modules\mongoskin\node_modules\mongodb\node_modules\bson>node "C
:\Program Files\nodejs\node_modules\npm\bin\node-gyp-bin\\..\..\node_modules\nod
e-gyp\bin\node-gyp.js" rebuild
npm http 304 https://registry.npmjs.org/bytes/0.2.0
npm http 304 https://registry.npmjs.org/formidable/1.0.14
npm http 304 https://registry.npmjs.org/send/0.1.1
npm http 304 https://registry.npmjs.org/pause/0.0.1
npm http 304 https://registry.npmjs.org/mime/1.2.6
npm http GET https://registry.npmjs.org/mime
npm http 304 https://registry.npmjs.org/uglify-js/2.4.0
npm http 304 https://registry.npmjs.org/promise
npm http 304 https://registry.npmjs.org/css
npm http 304 https://registry.npmjs.org/uglify-js
npm http 304 https://registry.npmjs.org/uglify-js
npm http GET https://registry.npmjs.org/uglify-to-browserify
npm http GET https://registry.npmjs.org/optimist
npm http GET https://registry.npmjs.org/source-map
npm http GET https://registry.npmjs.org/async
npm http GET https://registry.npmjs.org/is-promise
npm http GET https://registry.npmjs.org/css-parse/1.0.4
npm http GET https://registry.npmjs.org/css-stringify/1.0.5
npm http 304 https://registry.npmjs.org/readdirp
npm http 304 https://registry.npmjs.org/mime
npm http GET https://registry.npmjs.org/minimatch
npm http 304 https://registry.npmjs.org/is-promise
npm http 304 https://registry.npmjs.org/async
npm http 304 https://registry.npmjs.org/css-parse/1.0.4
npm http 304 https://registry.npmjs.org/css-stringify/1.0.5
npm http 304 https://registry.npmjs.org/source-map
npm http 304 https://registry.npmjs.org/uglify-to-browserify
npm http 304 https://registry.npmjs.org/optimist
npm http GET https://registry.npmjs.org/wordwrap
npm http GET https://registry.npmjs.org/amdefine
npm http 304 https://registry.npmjs.org/minimatch
npm http GET https://registry.npmjs.org/lru-cache
npm http GET https://registry.npmjs.org/sigmund
npm http 304 https://registry.npmjs.org/wordwrap
npm http 304 https://registry.npmjs.org/amdefine
npm http 304 https://registry.npmjs.org/lru-cache
npm http 304 https://registry.npmjs.org/sigmund
mongoskin@0.6.1 node_modules\mongoskin
└── mongodb@1.3.19 (kerberos@0.0.3, bson@0.2.2)

express@3.2.6 node_modules\express
├── methods@0.0.1
├── fresh@0.1.0
├── cookie-signature@1.0.1
├── range-parser@0.0.4
├── debug@0.7.4
├── buffer-crc32@0.2.1
├── cookie@0.1.0
├── commander@0.6.1
├── mkdirp@0.3.4
├── send@0.1.0 (mime@1.2.6)
└── connect@2.7.11 (qs@0.6.5, pause@0.0.1, bytes@0.2.0, cookie@0.0.5, formidable
@1.0.14, send@0.1.1)

jade@1.0.2 node_modules\jade
├── character-parser@1.2.0
├── commander@2.0.0
├── mkdirp@0.3.5
├── transformers@2.1.0 (promise@2.0.0, css@1.0.8, uglify-js@2.2.5)
├── monocle@1.1.50 (readdirp@0.2.5)
├── constantinople@1.0.2 (uglify-js@2.4.8)
└── with@2.0.0 (uglify-js@2.4.0)

D:\RestApp>

Now if you look at different things the npm command did, it actually took the dependencies in your package.json and imported them and any internally dependent libraries required for the modules under the node_modules folder structure in your application home directory. 

Once you have done this you can easily use the features / functionalists that is offered out of the box by the imported modules, how do we manually import it in your application is something that i would cover in my next NodeJS article.

Some popular NodeJS modules

  • Express - NodeJS modules for creating a web application
  • Jade - Jade template engine
  • mongoskin - Module to interact and communicate with MongoDB database
  • Cofee-Script - Unfancy javascript
  • Mocha - Test framework
  • Underscore - Helper library
  • Connect - High performance middle-ware framework  

Written by

We are Creative Blogger Theme Wavers which provides user friendly, effective and easy to use themes. Each support has free and providing HD support screen casting.

0 comments:

Post a Comment

 

© 2013 NimbleGeek. All rights resevered. Designed by Templateism

Back To Top