Understanding Peer Dependencies

Summary: declare a peer dependency when your package must share a single copy of another package with the application, as a plugin shares its host or a component library shares a UI framework. Keep the range as wide as you actually test, mark truly optional peers as optional, and treat raising a peer's minimum version as a breaking change.

Regular dependencies say "I need this, install a copy for me". Peer dependencies say "I need this, and I expect the project using me to provide it". The difference sounds small, but it decides whether an application ends up with one instance of a framework or two, and that is often the difference between working and mysteriously broken.

When a peer is the right choice

The classic cases share a pattern: your code plugs into something that must be a singleton in the application.

If your package simply calls a utility library internally, that is a normal dependency. Making it a peer only pushes work onto your users.

Writing the range

{
  "name": "example-plugin",
  "peerDependencies": {
    "host-tool": "^8.0.0 || ^9.0.0",
    "optional-helper": ">=2.1.0"
  },
  "peerDependenciesMeta": {
    "optional-helper": { "optional": true }
  },
  "devDependencies": {
    "host-tool": "^9.0.0"
  }
}

A few points in that snippet are deliberate. The host range spans the major versions you have actually tested; claim nothing wider. The host also appears in devDependencies so your own tests can run. And peerDependenciesMeta marks a peer as optional, which suits features that light up only when another package happens to be installed.

Why installs fail

Since npm 7, the npm client installs peer dependencies automatically and reports a conflict when it cannot find a version that satisfies every package's range. That is the familiar ERESOLVE error. pnpm and Yarn have their own, broadly similar behaviour with different defaults. Usually the cause is one of these:

  1. Your peer range has not been widened after the host shipped a new major.
  2. Two plugins in the same project declare non-overlapping ranges for the same host.
  3. The application pins an older host than your range allows.

Users can bypass the check with flags such as --legacy-peer-deps, but that hides the problem rather than solving it. As an author, the fix is almost always to test against the new host and widen the range in a release.

Versioning implications

Changes to peer ranges are changes to your contract. Widening a range is a compatible change and fits a minor or patch release. Narrowing it, or raising the minimum, can stop existing projects from installing your new version, so many maintainers treat it as major. Mention every peer-range change in the changelog, because users debugging an install error will look there first.

Related pages