Node Package Manager (NPM) Package manager

{
    "name": "my-app".// This is through -s which is needed in the production process
    "dependences": {
        "package-a": "^ 1.0.0"
    },
    // It is the package under -d, which is used during development
    "devDependencies": {
        "package-b": "^ 1.2.2." "}}Copy the code

// There is a less common one, specify
"peerDependencies": {
    "a": "^ 1.0.0"
}
Copy the code

peerDependencies

Let’s say our current project is MyProject, and there are dependencies in the project. For example, we have a dependency package PackageA, whose package.json file specifies PackageB dependencies:

{"dependencies": {"PackageB": "1.0.0"}}Copy the code

If we execute NPM install PackageA in our MyProject project, we will find that the directory structure of our project will look like this:

MyProject | - node_modules | - PackageA | - node_modules | - PackageB ` ` ` so in our project, we can through the following statements into "PackageA" :Copy the code

var packageA = require(‘PackageA’)

However, if you want to reference PackageB directly in your project:Copy the code

var packageA = require(‘PackageA’) var packageB = require(‘PackageB’)

This will not work, even if PackageB is installed; Because Node only looks for PackageB in MyProject/node_modules, it doesn't look for PackageB in node_modules. So, to solve this problem, in the MyProject project package.json we must directly declare the dependency on PackageB and install it. PackageA package.jsonCopy the code

{“peerDependencies”: {“PackageB”: “1.0.0”}}

So, it tells NPM that if a package lists me as a dependency, then that package must also have a dependency on PackageB. That is, if you NPM install PackageA, you will get the following directory structure:Copy the code

MyProject |- node_modules |- PackageA |- PackageB

In nPM2, the PackageB package is installed in the node_modules folder of the current project, even if the current project MyProject does not rely on PackageB directly. Summary: Specific functions of peerDependencies: The purpose of peerDependencies is to prompt the host environment to install packages that meet the dependencies specified in the plugin peerDependencies, and then always refer to the NPM package installed by the host environment when the plugin import or require the packages that depend on it. Finally solve the problem of inconsistency between plug-ins and dependent packages. This is very important for writing NPM packages. When you add a package to your peerDependencies, write it in peerDependencies to avoid multiple downloadsCopy the code