Installing modules using npm

The module system in Node is very powerful that consuming a third-party module written by other developers is a piece of cake. Node includes its own package manager called npm, which is a registry that currently contains over 475,000 modules written in Node. These modules are completely open source and available to you via a few short commands. In addition, you can release your own personal modules via npm and allow anyone in the world to use your feature!

Let's say you want to include a popular web framework, express, in your project (the one we will be using later in this book). There are two simple steps required to download a module and use it in your code:

    $ npm install express
    // ** file: usingnpm.js
    var express = require('express');  

And that's it! Literally, it's that simple! From the command line of the folder where your project is located, simply execute npm install package-name, and the package will be downloaded from npm and stored in a folder called node_modules within your project. If you browse through the node_modules folder, you will find a folder for the package you installed, and within that folder, you will find the raw source code for the package itself. Once the package is downloaded, it's as simple as using require() within your code.

There may be times when you want to install a Node package globally, for example, when using a popular command-line build tool called Grunt.js. To install an npm package globally, simply include the -g or --global flag, and the module will be installed as a global executable instead. When installing npm packages globally, the source files for the package are not stored within the node_modules folder of a specific project, but instead within a node_modules folder in a system directory of your machine.

A really powerful feature of npm is that it allows a quick, easy, and consistent way for other developers to boot up your code in their local environment. Node projects, typically, include a special file called package.json that contains information about the project as well as a list of all npm packages that the project depends on. A developer with a copy of your local code can simply execute npm install to have every dependency downloaded and installed locally using this file.

The npm install flag --save or --save-dev is required if you want the dependency that you are installing to be saved to the package.json file. If you are starting a new project and don't want to create a package.json file by hand, you can simply execute npm init and answer a few quick questions to get a default package.json file quickly set up. You can leave every question blank during init and accept the default values if you want:

    $ npm init
    
    $ npm install express --save
    $ npm install grunt --save-dev
    $ cat package.json
    {
     "name": "chapter3",
     "version": "0.0.0",
     "description": "",
     "main": "index.js",
     "scripts": {
       "test": "echo "Error: no test specified" && exit 1"
     },
     "author": "",
     "license": "ISC",
     "dependencies": {
       "express": "^3.5.1"
     },
     "devDependencies": {
       "grunt": "^0.4.4"
     }
    }
  
The dependencies and devDependencies sections have express and grunt listed. The difference between these two sections is that the dependencies section is absolutely critical for the app to function properly and the devDependencies section has only packages that need to be installed for a developer to use during the development of the project (such as Grunt for various build steps, testing frameworks, and so on). If you are confused about the use of the ^ symbol in the package versions, it's used to update the dependency to the most recent minor version or patch version (the second or third number). ^1.2.3 will match any 1.x.x release including 1.3.0, but will hold off on 2.0.0. So, in our case ^3.5.1 of Express.js will look for the most recent minor version of express.js but will not take 4.0.0, as it's a major version.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.35.247