What is the peer dependency in the node module?

A simple description of the peerDependencies field in the package.json file

In somepackage.jsonFile, you may see the following lines:

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

You may have seendependencieswithdevDependencies, but notpeerDependencies.

dependenciesAre the packages that your project depends on.

devDependenciesIt is a software package required during the development phase. Say a similar test frameworkjokeOr other utilities such asTower of BabelorESLint.

In both cases, when the package is installed, the dependencies and devDependencies of the package are automatically installednpm.

peerDependenciesIs different. They will not be installed automatically.

When the dependency is listed as peerDependency in the package,It won't install automatically. Instead, the code that contains the package must include it as its dependency.

npmWill warn you if you runnpm installAnd no such dependency was found.

Example: Let’s say a packageaIncluding dependenciesb:

a/package.json

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

packagebIn turn to packcAs peerDependency:

b/package.json

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

Therefore, in package A, we must addcAs a dependency, otherwise when installing the packageb, Npm will warn you (the code may fail at runtime):

a/package.json

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

The version must be compatible, so if peerDependency is listed as2.x, You cannot install1.xOr other versions. It all followsSemantic version control.

Download mine for freeNode.js manual


More node tutorials: