Articles

npm Peer Dependencies Explained: How peerDependencies Work

npm peer dependencies declare a host library the consumer must provide, like React for a plugin. How peerDependencies work and how to fix install conflicts.

The Lycoris Team The Lycoris Team · · 5 min read
Laptop screen showing JavaScript code in blue light

A peer dependency is a package that a library needs to work but expects the consuming project to provide, rather than installing its own copy. A React component library, an ESLint plugin, or a bundler plugin all list their host as a peer dependency: they run inside that host and must share the same instance. Declaring it under peerDependencies in package.json tells the package manager, “I need this, but use the project’s version — and make sure it’s one I’m compatible with.”

The problem peer dependencies solve

Normal dependencies are private to the package that declares them. If library A depends on lodash and library B depends on a different major version of lodash, the package manager can install both versions side by side, and each library gets its own. That isolation is usually exactly what you want.

It breaks down for packages that must be singletons. React is the canonical example: hooks rely on internal state tied to one copy of React. If a component library bundled its own React alongside your app’s React, components would fail with errors about invalid hook calls. Plugins have the same problem in a different form — an ESLint plugin needs to register with the ESLint instance that’s actually running, not a private copy sitting in its own node_modules.

Peer dependencies express that relationship. The library says which versions of the host it supports, and the project supplies one shared copy.

Declaring peer dependencies

A component library’s package.json might look like this:

{
  "name": "my-ui-kit",
  "peerDependencies": {
    "react": "^18.0.0 || ^19.0.0",
    "react-dom": "^18.0.0 || ^19.0.0"
  },
  "devDependencies": {
    "react": "^19.0.0",
    "react-dom": "^19.0.0"
  }
}

The peerDependencies range uses the same semantic versioning syntax as regular dependencies. Ranges should be as wide as you can genuinely support — every major version you’ve tested — because narrow peer ranges are the main cause of install conflicts for your users.

The library also lists the host under devDependencies so its own tests and build have a copy to run against. That copy is never installed for consumers.

Optional peers

Sometimes a library integrates with a host only if it’s present — a utility that adds TypeScript types or works with an optional framework adapter. peerDependenciesMeta marks a peer as optional so package managers don’t complain when it’s absent:

{
  "peerDependencies": { "typescript": ">=5" },
  "peerDependenciesMeta": { "typescript": { "optional": true } }
}

dependencies vs devDependencies vs peerDependencies

dependenciesdevDependenciespeerDependencies
Installed for consumersYesNoProvided by the consumer’s project
Copy per packageCan be private and duplicatedOnly in the package’s own dev setupOne shared copy
Typical useLibraries the code calls internallyTest runners, build tools, lintersHosts and frameworks: React, ESLint, bundlers
Version conflictsResolved by installing multiple copiesNot relevant to consumersReported as errors or warnings

A useful rule of thumb: if two copies of the package in the same app would break things, or if your package plugs into it rather than calling it, it should probably be a peer.

How package managers handle peers

Behavior has differed across package managers and versions, which explains much of the confusion around peer dependencies. Our npm vs pnpm vs Yarn comparison covers the broader differences.

npm (since version 7) installs peer dependencies automatically if the project doesn’t already have them, and treats an incompatible peer as an error. That’s the source of the familiar ERESOLVE unable to resolve dependency tree message: two packages in the tree demand peer ranges that can’t both be satisfied, or one demands a range your project’s version falls outside.

pnpm and Yarn generally report unmet or incompatible peers as warnings rather than hard failures, and pnpm can be configured to install missing peers automatically. pnpm’s strict, symlinked node_modules layout also makes peer resolution more explicit, which surfaces mistakes that a flat layout hides.

Whatever the tool, the resolved versions end up recorded in your lockfile, so a conflict that was forced through once will keep reappearing on fresh installs unless it’s fixed properly.

Fixing peer dependency conflicts

When an install fails on a peer conflict, read the error carefully. npm prints which package requested which range and what was found instead. Then work through the options in roughly this order:

  1. Upgrade the package with the narrow range. Often a newer release of the plugin already supports your host’s version.
  2. Align your host version. If several plugins agree on a range your project is outside of, moving the host may be the cleaner fix.
  3. Use overrides deliberately. npm’s overrides, Yarn’s resolutions, and pnpm’s equivalents let you force a version when you’ve verified compatibility yourself and the upstream range is simply out of date.
  4. Use --legacy-peer-deps as a last resort. This tells npm to ignore peer dependencies the way older versions did. It gets the install through, but it hides real incompatibilities and has to be repeated on every install, including in CI.

Avoid --force as a habit. It accepts conflicting trees rather than resolving them, and problems tend to surface later as confusing runtime errors.

Peer dependencies in monorepos

Monorepos are where peer dependencies matter most. A shared UI package and several apps must all agree on one React version, and internal packages that plug into a shared framework should declare it as a peer rather than a dependency. npm and pnpm workspaces hoist or link shared packages, but they can’t fix a package that wrongly lists React under dependencies — that’s how duplicate copies, and the bugs that come with them, creep in.

The takeaway

Peer dependencies declare that a package runs inside a host — React, ESLint, a bundler — that the consuming project must provide as a single shared copy. Library authors should list hosts as peers with the widest tested version range, plus a dev dependency for local work. App developers hitting peer conflicts should upgrade or align versions first, use overrides only after verifying compatibility, and treat --legacy-peer-deps as a temporary workaround rather than a fix.

Takina Takina · · 4 min read

What Is a JavaScript Bundler? How Bundling Works

A JavaScript bundler combines source modules and their dependencies into optimized files a browser can load efficiently. How bundling works, step by step.

#JavaScript #Web Development #Developer Tools
Takina Takina · · 4 min read

npm and pnpm Workspaces: Managing a Monorepo

Workspaces let npm and pnpm manage multiple packages in one repository, sharing a single dependency tree and letting packages reference each other locally.

#JavaScript #Web Development #Developer Tools