Overview
Under each project's root directory, there is generally a Package.json file that defines the various modules needed for the project, as well as the project's configuration information (such as name, version, license, etc.). The NPM install command automatically downloads the required modules based on this profile, which is the running and development environment required to configure the project.
The following is a simplest Package.json file that defines only two metadata: the project name and the project version.
{
' name ': ' xxx ',
' version ': ' 0.0.0 ',
}
The code above shows that a JSON object is inside the Package.json file, and each member of that object is a setting for the current project. For example, name is the project name, version is the edition (following the format of "large version. Minor version. Iteration").
The following is a more complete Package.json file.
{"name": "Hello World", "Version": "0.0.1", "Author": "John", "description": "First Node.js program", "keywords": ["Node.js", "J Avascript "]," repository ": {" type ":" Git "," url ":" Https://path/to/url "}," license ":" MIT "," engines ": {" node ":" 0.10.x "}," Bugs ": {" url ":" Http://path/to/bug "," email ":" Bug@example.com "}," contributors ": [{" Name ":" Dick "," Email ":" Lisi@example.com "}]," 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", "GRU
Nt-contrib-uglify ":" ~0.2.7 "," Grunt-contrib-clean ":" ~0.5.0 "," browserify ":" 2.36.1 "," grunt-browserify ":" ~1.3.0 ", }
}
The fields of the Package.json file are explained in detail below. scripts Field
scripts specifies the NPM command line abbreviation for running script commands, such as start specifies the command to execute when NPM run start is running.
The following settings specify the commands to be executed when NPM run preinstall, NPM run Postinstall, npm run start, NPM run test.
"Scripts": {"
Preinstall": "Echo here it comes!",
"Postinstall": "Echo There it goes!",
"Start": "Node index." JS ",
" test ":" Tap Test/*.js "
}
dependencies fields, devdependencies fields
The dependencies field specifies the module on which the project will run, devdependencies the modules required for project development.
They all point to an object. Each member of the object, consisting of the module name and the corresponding version requirement, represents the dependent module and its version range.
{
"devdependencies": {"
browserify": "~13.0.0",
"karma-browserify": "~5.0.1"
}
}
The corresponding version can be combined with a variety of restrictions, mainly the following: The specified version: For example, 1.2.2, follow the "large version. Minor version," The format of the installation, install only the specified version. Wave (tilde) + specified version: ~1.2.2, for example, indicates the most recent version of 1.2.x installed (not less than 1.2.2), but does not install 1.3.x, which means that the large and minor version numbers are not changed during installation. Insert Number (caret) + specified version: For example, ˆ1.2.2, the latest version of the installation 1.x.x (not less than 1.2.2), but does not install 2.x.x, which means that the installation does not change the large version number. It is important to note that if the large version number is 0, the insertion number behaves the same as the wave number, because it is at the development stage, and even minor version number changes may result in incompatibility of the program. Latest: Install the latest version.
Package.json files can be written manually or automatically generated using the NPM init command.
$ NPM Init
This command is interactive, asking the user to answer some questions and then generating a basic Package.json file in the current directory. Of all the issues, only the project name and project version are required, and the rest are optional.
With the Package.json file, the NPM Install command is used directly to install the required modules in the current directory.
$ NPM Install
If a module is not in the Package.json file, you can install the module separately and use the appropriate parameters to write it to the Package.json file.
$ npm Install Express--save
$ npm Install Express--save-dev
The code above indicates that the Express module is installed separately, and the--save parameter indicates that the module is written to the Dependencies property,--save-dev that the module is written to the Devdependencies attribute. peerdependencies
Sometimes, your project and the modules you rely on are dependent on another module, but the version you rely on is different. For example, your project relies on version 1.0 of module A and Module B, while the A module itself relies on version 2.0 of the B module.
In most cases, this does not pose a problem, and the two versions of Module B can coexist and run simultaneously. However, there is a situation where the problem is that the dependency will be exposed to the user.
The most typical scenario is the plug-in, for example, a module is a B-module plug-in. The B module installed by the user is version 1.0, but the a plugin can only be used with the 2.0 version of the B module. The problem arises when the user passes an instance of version 1.0 b to a. Therefore, a mechanism is needed to alert the user when the template is installed, if A and B are installed together, then B must be a 2.0 module.
The Peerdependencies field is the version used by the plug-in to specify the main tool that it needs.
{
' name ': ' chai-as-promised ',
' peerdependencies ': {
' chai ': ' 1.x '
}
}
The code above specifies that when the chai-as-promised module is installed, the main program chai must be installed together and the Chai version must be 1.x. If your project specifies that the dependency is Chai version 2.0, you will be given an error.
Note, starting with NPM 3.0, peerdependencies