The content of this document is what you must know, it must be in JSON text format. Under the root directory of each project, there is usually a package.json file that defines the various modules required for the project, as well as the configuration information (such as name, version, license, and so on) of the project. The npm install command automatically downloads the required modules based on this configuration file, which is the run and development environment required to configure the project.
Name and Version properties
This property is mandatory, defining the name of the project and the version number of the project (following the "large version. Minor version" format). If you do not have the Name property and the Version property, the package will not be installed. Name and version guarantee the uniqueness of the package.
Attention:
- Name is not more than 240 bytes in length;
- Name cannot start with dots and underscores;
- Name can only be lowercase;
Tips:
- Cannot be an existing package name (see NPM Registry, if this name exists);
- Cannot have JS or node in name;
- as short as possible;
Dependencies Property
dependencies属性Specifies the module on which the project runs, devDependencies specifying the modules that are required for project development. They all point to an object. Each member of the object consists of a module name and a corresponding version requirement, which represents the dependent module and its version range.
The corresponding version can be combined with a variety of qualifications, mainly in the following categories:
-
1. Specify the version: for example 1.2.2, follow the format of "large version. Minor version." Minor version specifies that only the specified version is installed.
2. Greater than (>), greater than or equal (>=), less than (<), less than or equal to (<=): Must be greater than (greater than or equal to, less than, less than or equal to) a version, can also be used (>=version1 <=version2).
3. Asterisk (*): any version.
4. Git version (Git ... ): You can use the Git URL to make version dependencies. 5. Tilde (tilde) + specified version: ~1.2.2, for example, installs the latest version of 1.2.x (no 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. 6. Insert Number (caret) + specified version: For example, 1.2.2, indicates the latest version of the installation 1.x.x (not less than 1.2.2), but does not install 2.x.x, that is, the installation does not change the large version number. It is important to note that if the large version number is 0, the caret behaves the same as the tilde, because at this point in the development phase, even minor version number changes can lead to incompatible programs. 7. Latest: Install the latest version.
Example:
Package.json files can be written manually, or they can be npm init generated automatically using commands.
$ NPM Init
This command takes an interactive approach, asks the user to answer some questions, and then generates a basic Package.json file in the current directory. Of all the issues, only the project name and project version (versions) are required, and the others are optional.
With the Package.json file, directly using the NPM Install command, the required modules are installed in the current directory.
$ NPM Install
If a module is not in package.json the file, you can install the module separately and use the corresponding parameters to write it to the package.json file.
$ NPM Install Express----save-dev
The above code indicates that the Express module is installed separately, and the parameter indicates that the module is written to the property --save dependencies , --save-dev indicating that the module is written to the devDependencies property.
Description property
The Description property is a description of the project that helps the user discover your project.
License Property
The license property is a representation of what rules your project publishes, and what permissions the user has.
{"License": "Bsd-3-clause"}
Author Property
The Author property indicates the publisher of the project.
Ahthor: "Zhang San"
Contributions Property
The contributions attribute indicates the contributor to the project.
"Contributors": [{"Name": "John Doe", "email": "[email protected]"}],
Scripts Property
scriptsSpecifies the NPM command line abbreviation that runs the script command, such as start specifying the npm run start command to execute at run time.
The following settings specify,,,, and when, the npm run preinstall npm run postinstall npm run start npm run test command to execute.
"Scripts": { "preinstall": "Echo here it comes!", "Postinstall": "Echo There it goes!", "Start": "Node index." JS ", " test ":" Tap Test/*.js "}
Peerdependencies Property
Sometimes your project and the modules you rely on depend on another module, but the versions you rely on are different. For example, your project relies on version 1.0 of the A module and the B module, and 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 the B module can coexist and run concurrently. However, there is a situation where the problem arises that this dependency is exposed to the user.
The most typical scenario is a plug-in, for example a module is a plug-in for a B module. The user-installed B module is version 1.0, but a plugin can only be used with the 2.0 version of the B module. At this point, if a user passes an instance of version 1.0 of B to a, there will be a problem. Therefore, a mechanism is needed to alert the user when a template is installed, if A and B are installed together, then B must be a 2.0 module.
peerDependenciesfield is the version of the main tool that is used by the plug-in to specify its needs.
{ " name ": " chai-as-promised " , " peerdependencies : { " Chai ": " 1.x }}
The code above specifies that chai-as-promised when the module is installed, the main program chai must be installed together, and chai the version must be 1.x . If your project specifies a dependency chai of 2.0 version, an error will be given.
Note that starting with NPM version 3.0, it peerDependencies is no longer installed by default.
Bin Property
The bin entry is used to specify the location of the executable file for each internal command.
" bin " : { "sometool""./bin/sometool.js" }
The above code specifies that the executable file corresponding to the Sometool command is sometool.js under the bin subdirectory. NPM will look for this file and node_modules/.bin/ create a symbolic link in the directory. In the example above, Sometool.js will create symbolic links npm_modules/.bin/someTool . Because the node_modules/.bin/ directory joins the system's path variable at run time, it is possible to invoke these scripts directly from the command without a path when running NPM.
Main Property
mainfield specifies the loaded portal file, require(‘moduleName‘) the file is loaded. The default value for this field is under the module root directory index.js .
Config Property
config属性The environment variable that is used to add the command line.
Here is a package.json file.
{ "name":"Foo", "Config": {"Port":"8080" }, "Scripts": {"Start":"node Server.js" }}
The server.js value of the field can then be referenced in the script config .
HTTP . Createserver (...) . Listen (Process.env.npm_package_config_port)
npm run startthe script can get the value when the user executes the command.
$ NPM Run Start
The user can change this value.
Set the
Engines Property
enginesThe field indicates the platform that the module is running on, such as a version of Node or a browser.
"Engines": {"node": "0.10.x"},
Repository Property
The Repository property indicates your warehouse location and helps users iterate over your project.
For example:
" Repository " : {"type""git" ,"URL ""https://github.com/npm/npm.git"}
Package.json effect