pnpm: Problem with peer dependencies when using `pnpm link`

When a package that has peer dependencies is linked to some destination, the node_modules of that package are included in the symbolic link. This causes the dependencies of the package to come from another source than the destination’s node_modules/.pnpm folder. This becomes a problem with peer dependencies, because they end up not pointing to the same exact file. Even when the version on both side is an exact match, this can lead to problems when exact references (e.g.: singleton) are needed.

The simple example is for a React package, where React is marked as a peer dependency. When a consumer package links the first library, there will be a missing in the imported files, leading to countless problem on runtime.

pnpm version:

8.6.3

Code to reproduce the issue:

I’ve created a reproduction repository that includes a README.md file with all the instructions needed in order to get a better feel of the issue.

https://github.com/kevinpastor/pnpm-link-peers-issue

Expected behavior:

When linking a package that has peer dependencies to some destination, we should expect that the peer dependencies are respected.

Actual behavior:

The peer dependencies of the package behave more like dependencies. The peer dependencies are consumed from the package’s original location and not from the destination’s node_modules.

Additional information:

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 17
  • Comments: 18 (6 by maintainers)

Most upvoted comments

I tested the file: protocol in our actual codebase and it works as we would expect. I ended up using the file: protocol directly in the dependencies property since the library we are developping isn’t yet available on the package repository.

That being said, I do think that the issue for pnpm link still applies. The solution you proposed requires us to rebuild and reinstall the library everytime we make a single change to it. Looking at the documentation of npm link, it mentions that “[it] is handy for installing your own stuff, so that you can work on it and test iteratively without having to continually rebuild” (source). Having that in mind, I would expect pnpm link to have somewhat of the same motto. Not having proper support for peer dependencies is a big shortcoming to me.

You cannot solve this with pnpm link. What you can do is use a file: protocol. If it is inside a workspace, you could also use injected dependencies.

In your case, if you need this for local development and the projects are in separate directories, probably the file: protocol would be the way. You can put it into an override like so:

{
  "name": "consumer",
  "version": "1.0.0",
  "dependencies": {
    "dep": "1.0.0"
  },
  "pnpm": {
    "overrides": {
      "dep": "file:../dep"
    }
  }
}

I know, we need better docs. The most upvoted issue is actually related to linking: https://github.com/pnpm/pnpm/issues/4640

The only point of friction I have with the file: protocol comes from our usage of TypeScript. If some-library is written in TypeScript and builds to dist/, the files attribute of its package.json with probably look like ["dist/"]. This ends up cleaning up the hard link meaning that file changes do not end up in some-consumer without a reinstall.

Any idea on what could be done in order to circumvent this?

Dependencies added via the file: protocol are already hard linked. So if you edit a file, it will be changed in the consumer. But if you build your package, then the files in dist are probably replaced, so it doesn’t matter that they were hard links.