官术网_书友最值得收藏!

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

主站蜘蛛池模板: 宜春市| 靖边县| 博爱县| 西乌珠穆沁旗| 渭南市| 宁南县| 永川市| 灵川县| 专栏| 厦门市| 铁力市| 于田县| 扶绥县| 黑水县| 金昌市| 巴彦县| 县级市| 阿图什市| 岳阳市| 怀化市| 始兴县| 绩溪县| 宜州市| 尚义县| 黔江区| 凤山县| 红桥区| 呼玛县| 那坡县| 剑川县| 淳安县| 收藏| 临朐县| 班戈县| 临海市| 汉川市| 福海县| 汉沽区| 罗平县| 鲁山县| 乳山市|