Package. The json file

directory

  • An overview of the
  • Scripts field
  • Dependencies field, devDependencies field
  • peerDependencies
  • Bin field
  • The main field
  • The config field
  • other
  • Browser field
  • Engines field
  • Man field
  • PreferGlobal field
  • Style field

An overview of the

Under the root of each project, there is typically 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, and other metadata). Based on this configuration file, the NPM install command automatically downloads the required modules, that is, the runtime and development environment required to configure the project.

Here is the simplest package.json file that defines only two pieces of metadata: the project name and the project version.

{"name" : "XXX ", "version" : "0.0.0",}Copy the code

A package.json file is a JSON object, and each member of that object is a setting for the current project. For example, name is the project name and version is the version. Minor version. Minor version “).

Here is a more complete package.json file.

{" name ":" Hello World ", "version" : "0.0.1", "author" : "zhang", "description" : "The first node. Js program", "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 ":" bill ", "email" : "[email protected]"}], "scripts" : {" start ":" node index. The js "}, "dependencies" : {" express ":" the 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,"}}Copy the code

The fields of the package.json file are explained in detail below.

Scripts field

Scripts specifies the NPM command line abbreviation for running the script command. For example, start specifies the command to be executed when NPM run start is run.

The following Settings specify the commands to be executed when NPM run preinstall, NPM run postinstall, NPM run start, and NPM run test are specified.

"scripts": { "preinstall": "echo here it comes!" , "postinstall": "echo there it goes!" , "start": "node index.js", "test": "tap test/*.js" }Copy the code

Dependencies field, devDependencies field

The Dependencies field specifies the modules on which the project runs, and devDependencies specifies the modules needed to develop the project.

They all point to an object. Each member of this object, consisting of a module name and corresponding version requirements, represents the dependent module and its version range.

{" devDependencies ": {" browserify" : "~ 13.0.0", "karma - browserify" : "~ 5.0.1"}}Copy the code

Various restrictions can be added to the corresponding version, including the following:

  • Specify the version: for instance,1.2.2, follow the “big version. Minor version. Minor version specifies that only the specified version will be installed during installation.
  • Tilde + specified version: for instance,~ 1.2.2, the latest version (not less than 1.2.2) of 1.2.x is installed, but 1.3.x is not installed, that is, the major and minor versions are not changed during installation.
  • We repeat that we install the latest version of 1.x.x (at least 1.2.2), but we do not install 2.x.x, that is, we do not change the larger version number when we install. It is important to note that if the major version number is 0, the behavior of the insertion number is the same as that of the tilde. This is because at this stage of development, even minor version number changes can cause incompatibilities in the program.
  • Latest: Installs the latest version.

The package.json file can be written manually or generated automatically using the NPM init command.

$ npm init
Copy the code

This command takes an interactive approach, asking the user to answer some questions and then generating a basic package.json file in the current directory. Of all the questions, only the project name and project version are mandatory, the others are optional.

With the package.json file, using the NPM install command directly will install the required modules in the current directory.

$ npm install
Copy the code

If a module is not in the package.json file, you can install the module separately and write it to the package.json file with the appropriate parameters.

$ npm install express --save
$ npm install express --save-dev
Copy the code

The code above shows that the Express module is installed separately, with the –save argument writing the module to the Dependencies property and –save-dev writing the module to the devDependencies property.

peerDependencies

Sometimes, both your project and the module that you depend on will depend on another module at the same time, but in different versions. For example, your project relies on version 1.0 of module A and module B, and module A itself relies on version 2.0 of module B.

In most cases this is not a problem and both versions of the B module can coexist and run at the same time. There is a situation, however, where the problem is that this dependency is exposed to the user.

The most typical scenario is A plug-in, such as module A being A plug-in for module B. The user installed module B is version 1.0, but plug-in A can only be used with module B of version 2.0. At this point, users will have problems if they pass an instance of version 1.0 B to A. Therefore, A mechanism is needed to remind the user when the template is installed that if A and B are installed together, then B must be A 2.0 module.

The peerDependencies field is used by the plug-in to specify the version of the main tool that it needs.

{
  "name": "chai-as-promised",
  "peerDependencies": {
    "chai": "1.x"
  }
}
Copy the code

The code above specifies that when the chai-as-Promised module is installed, the main program CHAI must be installed together, and the version of CHAI must be 1.x. If your project specifies a chai 2.0 dependency, an error will be reported.

Note that starting with NPM 3.0, peerDependencies are no longer installed by default.

Bin field

The bin item is used to specify the location of the executable file corresponding to each internal command.

"bin": {
  "someTool": "./bin/someTool.js"
}
Copy the code

The executable file corresponding to the someTool command is sometool.js in the bin subdirectory. Npm looks for this file and makes a symbolic link under node_modules/.bin/. In the example above, sometool. js creates a symbolic link to npm_modules/.bin/someTool. Since the node_modules/.bin/ directory adds the system’s PATH variable at run time, these scripts can be invoked directly with commands without paths when running NPM.

Therefore, something like the following can be abbreviated.

Scripts: {start: ', / node_modules/someTool someTool js build '} / / shorthand for scripts: {start: 'someTool build'}Copy the code

All commands in node_modules/. Bin/can be run in the NPM run [command] format. On the command line, type NPM run and press TAB to display all available commands.

The main field

The main field specifies the entry file to load, which requires (‘moduleName’) will load. The default value for this field is index.js under the module root.

The config field

The config field is used to add command line environment variables.

Here is a package.json file.

{
  "name" : "foo",
  "config" : { "port" : "8080" },
  "scripts" : { "start" : "node server.js" }
}
Copy the code

The value of the Config field can then be referenced in the server.js script.

http
  .createServer(...)
  .listen(process.env.npm_package_config_port)
Copy the code

The script gets the value when the user executes the NPM run start command.

$ npm run start
Copy the code

The user can change this value.

$ npm config set foo:port 80
Copy the code

other

Browser field

Browser Specifies the version of this template for browser use. A browser packaging tool like Browserify knows which file to package.

"browser": {
  "tipso": "./node_modules/tipso/src/tipso.js"
},
Copy the code

Engines field

The Engines field specifies the platform on which the module is running, such as a version of Node or a browser.

{"engines" : {"node" : ">=0.10.3 <0.12"}}Copy the code

This field can also specify the applicable NPM version.

{"engines" : {" NPM ": "~1.0.20"}}Copy the code

Man field

Man specifies the location of the MAN document for the current module.

"man" :[ "./doc/calc.1" ]
Copy the code

PreferGlobal field

The preferGlobal value is a Boolean value indicating whether a warning should be displayed when the user does not install the module as a global module (i.e., without the -global parameter) indicating that the module is intended to be installed as a global module.

Style field

Style specifies the location of the style file for use by the browser. Style file packaging tool Parcelify, which knows where to package style files.

"style": [
  "./node_modules/tipso/src/tipso.css"
]
Copy the code