Understanding Peer Dependencies
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.
- A plugin for a build tool, linter or test runner, which must register with the host the application actually runs.
- A component library for a UI framework, where two copies of the framework would mean two separate rendering runtimes.
- An adapter that extends classes or hooks from another library, where
instanceofchecks or shared registries require the same copy.
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:
- Your peer range has not been widened after the host shipped a new major.
- Two plugins in the same project declare non-overlapping ranges for the same host.
- 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.