Skip to main content
Version: 4.x

How peers are resolved

One of the very great features of pnpm is that in one project, a specific version of a package will always have one set of dependencies. There is one exclusion from it though - packages with peer dependencies.

Peer dependencies are resolved from dependencies installed higher in the dependency graph. That means if [email protected] has two peers ([email protected]^1 and [email protected]^1) then it might have different sets of dependencies in the same project.

In the example above, [email protected] is installed for foo-parent-1 and foo-parent-2. Both packages have bar and bazas well, but they depend on different versions of baz. As a result, foo[email protected] has two different sets of dependencies: one with [email protected] and the other one with [email protected]. To support these use cases, pnpm has to hard link [email protected] as many times as many different dependency sets it has.

Normally, if a package does not have peer dependencies, it is hard linked to a node_modules folder next to symlinks of its dependencies.

However, if foo has peer dependencies, there cannot be one single set of dependencies for it, so we create different sets, for different peer dependency resolutions:

We create symlinks either to the foo that is inside [email protected][email protected]/node_modules or to the one in [email protected][email protected]/node_modules. As a consequence, the Node.js module resolver algorithm will find the correct peers.

If a package has no peer dependencies but has dependencies with peers that are resolved higher in the graph, then that transitive package can appear in the project with different sets of dependencies. For instance, there's package [email protected] with a single dependency [email protected]. [email protected] has a peer dependency [email protected]^1. [email protected] will never resolve the peers of [email protected], so it becomes dependent from the peers of [email protected] as well.

Here's how it will look like in node_modules/.registry.npmjs.org, in case if [email protected] will need to appear twice in the project's node_modules, once resolved with [email protected] and once with [email protected].