Original text: flaviocopes.com/npm-peer-de…

In some package.json files, you might see configuration lines like this:

{
  //...
  "peerDependencies": {
    "libraryName": "1.x"
  }
}

Copy the code

Dependencies and devDependencies are common, but peerDependencies are not.

Dependencies is the package that your project depends on.

DevDependencies is the package required for the development phase. Examples include testing frameworks like Jest or other libraries like Babel or ESLint.

In both cases, when you install a package, its dependencies and devDependencies are automatically installed by NPM.

PeerDependencies are different in that they are not installed automatically.

When a dependency C is listed in a package B’s peerDependency, it is not installed automatically. Instead, library A containing package B must include the corresponding dependency C as its dependency.

If the dependency is not found when you run NPM Install, NPM will warn you, as shown in the example:

a/package.json

{
  //...
  "dependencies": {
    "b": "1.x"
  }
}
Copy the code

b/package.json

{
  //...
  "peerDependencies": {
    "c": "1.x"
  }
}

Copy the code

Therefore, in package A, you must add C as a dependency so that when you install package B, NPM will not be alerted (and the code will not fail when running) :

a/package.json

{
  //...
  "dependencies": {
    "b": "1.x",
    "c": "1.x"
  }
}

Copy the code

Note that versions of dependencies must be compatible, if a peerDependency is marked 2.x, you cannot install 1.x or other incompatible versions. The regulations follow flaviocopes.com/npm-semanti… Standard.






–End–






View more front-end good article please search fewelife concern public number reprint please indicate the source