Peer dependency

On this page 7

A peer dependency is a declaration in a software package stating that it expects the consuming project to provide a specific package and version range, instead of installing a private copy of that package itself. In software installation and package management, it is the standard way for plugins and extensions to share one instance of a host library with the application that uses them.

The concept is most closely associated with the JavaScript ecosystem, where npm, Yarn and pnpm read the peerDependencies field of package.json. Similar ideas exist elsewhere, but the term itself and its tooling behavior are defined by the Node.js package managers.

How a peer dependency works

A regular dependency is owned by the package that declares it: the package manager installs it in the dependency tree beneath that package, and several versions can coexist. A peer dependency reverses the ownership. The declaring package says, in effect, “I work with your copy of this library”, and the package manager must satisfy the requirement at the level of the host project.

This matters whenever a library depends on shared runtime state or object identity. A React component library, for example, must use the same React instance as the application; two copies would break hooks and context. An ESLint plugin must register with the exact ESLint core that loads it. Declaring the host as a peer guarantees a single shared copy in the installed tree.

During dependency resolution, the package manager collects every peer requirement, compares it with the version the project actually provides and reports a peer dependency conflict when the ranges cannot be satisfied together.

FieldWho installs itTypical purpose
dependenciesInstalled with the packageCode the package needs at runtime
devDependenciesOnly in the package’s own developmentBuild, test and lint tools
peerDependenciesProvided by the host projectShared host library or framework
optionalDependenciesInstalled if possible, failure toleratedPlatform-specific accelerators
peerDependenciesMetaNot installed, modifies peersMarks a peer as optional
Dependency fields in package.json compared

How package managers handle peer dependencies

Package managers have treated peer dependencies very differently over time, which explains much of the confusion around the concept. The key question is whether a missing peer is installed automatically, only warned about, or treated as an error.

Package managerMissing peerConflicting peer
npm 3 to 6Warning only, not installedWarning only
npm 7 and laterInstalled automaticallyInstall fails with an ERESOLVE error
Yarn ClassicWarning onlyWarning only
Yarn BerryWarning, strict resolutionWarning; peer must be provided explicitly
pnpmInstalled automatically in recent versionsReported, configurable as strict
Peer dependency behavior by package manager

The change in npm 7 was the most disruptive. Projects that had installed cleanly for years suddenly failed with ERESOLVE, because long-ignored conflicts became fatal. The common workaround, the --legacy-peer-deps flag, restores the old behavior but hides genuine incompatibilities rather than fixing them.

pnpm adds a further subtlety: because it builds a strict, symlinked node_modules layout, a package can resolve to different copies depending on its peers. pnpm therefore creates separate virtual instances of a package for each distinct set of peer versions.

Writing correct peer dependency ranges

The declared range is a compatibility promise to every consumer, so it should be as wide as the tested support and no wider. A range that is too narrow forces conflicts on users; a range that is too wide hides real breakage.

Range patternExampleEffect on consumers
Single major caret^18.0.0Works with any 18.x release
Multiple majors^17.0.0 || ^18.0.0Supports a migration window
Open lower bound>=16.8.0Accepts untested future majors
Exact pin18.2.0Conflicts with almost every project
Wildcard*No protection against incompatible versions
Peer range patterns and their effects

Library authors usually list the same package in devDependencies as well, so that their own test suite has a concrete version to run against. Peers that enhance but are not essential should be marked optional through peerDependenciesMeta, which suppresses missing-peer warnings for consumers who do not use that feature.

A peer dependency is one specific form of software dependency, defined by who supplies it rather than by what it does. It interacts closely with transitive dependencies, version pinning and the dependency lock file.

  • Transitive dependency: a dependency of a dependency, installed automatically; a peer is deliberately not installed privately by the package that declares it.
  • Dependency lock file: records the resolved version of each peer, so a passing install is reproducible across machines.
  • Version pinning: fixing an exact version in the host project is often the practical way to satisfy several plugins’ peer ranges at once.
  • Dependency hell: incompatible peer ranges across plugins are one of the most common modern causes of unresolvable installs.

Other ecosystems solve the same problem differently. Python and Ruby install one version per environment by design, so every dependency already behaves like a shared peer, while Maven and Gradle use provided scope for libraries that the runtime container supplies.

Common peer dependency problems

Most failures arise when several packages declare incompatible ranges for the same peer, or when a framework releases a new major version before its plugin ecosystem catches up.

SymptomLikely causeRecommended fix
ERESOLVE on installTwo plugins require different peer majorsUpgrade or replace the lagging plugin
Invalid hook call at runtimeDuplicate copies of a UI frameworkDeduplicate and declare it as a peer
Plugin not recognizedPlugin bound to another core instanceInstall the core at the project root
Warnings flood the logOptional peers not marked optionalUse peerDependenciesMeta in the library
Symptoms, causes and fixes

The most important warning is that forcing an install is not a fix. Flags such as --force or --legacy-peer-deps let the installation finish, but the resulting tree may combine versions that were never tested together, and the failure moves from install time to production.

Understanding it simply

Imagine buying a lens that fits only a certain camera body. The lens does not come with its own camera; it expects you to already own the right one. A peer dependency works the same way: an add-on piece of software says which main program it needs, and the project must supply that program itself.

This keeps everything using one shared copy of the main program instead of several copies that might disagree. When two add-ons expect different versions of the same main program, the installer complains, and someone has to decide which versions fit together.

Applied in practice

A front-end team maintains a web dashboard built on React 17 and wants to upgrade to React 18. After changing the version, npm install stops with an ERESOLVE peer dependency conflict: a charting library and a date-picker both declare react as a peer with the range ^16.8.0 || ^17.0.0.

Instead of using --legacy-peer-deps, the team checks each library’s changelog. The charting library already has a release supporting React 18, while the date-picker is unmaintained, so they replace the date-picker with an actively maintained alternative. The install then succeeds without overrides, the lock file records a single React 18 instance, and the upgrade reaches production after two days of testing with no runtime hook errors.