Web Development with MongoDB and Node.js
上QQ阅读APP看书,第一时间看更新

Installing modules using npm

The module system in Node is so 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 60,000 unique 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 wanted to include the popular web framework Express in your project (the one we will be using later in this book). There are simply two 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 browsed to 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() from 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 for 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 includes 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 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"
 }
}

Note that 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. 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).