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:
node -vprints: v18.16.0- Windows, macOS, or Linux?: Windows
- Organization discussion
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 17
- Comments: 18 (6 by maintainers)
I tested the
file:protocol in our actual codebase and it works as we would expect. I ended up using thefile:protocol directly in thedependenciesproperty 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 linkstill 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 ofnpm 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 expectpnpm linkto 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 afile: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: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. Ifsome-libraryis written in TypeScript and builds todist/, thefilesattribute of itspackage.jsonwith probably look like["dist/"]. This ends up cleaning up the hard link meaning that file changes do not end up insome-consumerwithout 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.