chapter
Node.js project Package.json
file is the core of the application. Package.json
A file is saved profile project metadata, usually found in the root file Node.js application folder, npm install
an order under this configuration file, automatically download the required module, which is required to configure and run the project development environment .
Package.json format
Package.json file contents generally as follows.
{
"name": "Hello World",
"version": "0.0.1",
"author": "qikegu.com",
"description": "第一个node.js程序",
"keywords":["node.js","javascript"],
"repository": {
"type": "git",
"url": "https://path/to/url"
},
"license":"MIT",
"engines": {"node": "0.10.x"},
"bugs":{"url":"http://path/to/bug","email":"[email protected]"},
"contributors":[{"name":"李四","email":"[email protected]"}],
"scripts": {
"start": "node index.js"
},
"dependencies": {
"express": "latest",
"mongoose": "~3.8.3",
"handlebars-runtime": "~1.0.12",
"express3-handlebars": "~0.5.0",
"MD5": "~1.2.0"
},
"devDependencies": {
"bower": "~1.2.8",
"grunt": "~0.4.1",
"grunt-contrib-concat": "~0.3.0",
"grunt-contrib-jshint": "~0.7.2",
"grunt-contrib-uglify": "~0.2.7",
"grunt-contrib-clean": "~0.5.0",
"browserify": "2.36.1",
"grunt-browserify": "~1.3.0",
}
}
As described above, package.json
the file contains the configuration information of various items. Let's take a look at some general information:
- The Name : Application Name
- Version : version of the application
- The Description : Description of the application information
- Main : import documents specified load, require ( 'moduleName') will load the file. The default value of this field is the root directory module index.js.
- Scripts : npm specified command-line abbreviation run script commands, such as start time specifies the command to run npm run start, to be executed.
- Author : Developers
- License : License
- The Dependencies : specifies the operation of the project depends module
- DevDependencies module specified project development need:
- Repository : information and Web site application code resides on the repository, etc.
- Bugs : bug reporting application URL or e-mail
Package.json use
package.json files can be written by hand, you can also use the npm init
command automatically generated.
$ npm init
This command takes interactive mode requires the user to answer some questions and then generates a basic directory of the files in the current package.json. Of all the questions, only the project name (name) and project version (version) is mandatory, the others are optional.
With package.json documents directly using npm install
a command, it will install the required module in the current directory.
$ npm install
If the module is not in a file package.json, this module can be installed separately, and use the appropriate parameters, it is written into the file package.json.
$ npm install express --save
$ npm install express --save-dev
The above code represents express module installed separately, --save
the parameter indicates the module writes dependencies
attribute --save-dev
indicates the module writes devDependencies
attribute.