How to use npm link with pnpm and handle overwrites?
If you're working on a larger project with multiple packages, you might want to use pnpm for better performance and efficiency.
"pnpm": { "overrides": { "my-package-foo": "3.4.0" } }
What is pnpm?
- pnpm is an alternative to npm (Node Package Manager), designed to be faster and more efficient.
- When I first started using pnpm, I ran into issues while trying to link local packages where my main project used pnpm and other local packages used the standard npm. When I attempted to link packages using npm link or pnpm link, I frequently encountered errors stating that the package did not exist.
- To address this, I’ve outlined some steps for successfully using package linking with pnpm and handling overrides.
Steps:
-
Linking with npm: In the repository where you are using npm, type npm link. Then, go to your main project, where the linked package is used, and run the following command:
npm link <package-name-from-package.json>
-
That’s it for the repository using npm.
-
Linking with pnpm (with overrides): If you're using pnpm with overrides, and you want to override the package version, modify the package.json like this:
"overrides": "link:../foo-basic"
-
Make sure to point the path to the package directory, not the dist folder. Then, run pnpm install (pnpm i) to apply the changes.
Note:
If you're using VS Code, you may need to reload your IDE after linking packages. You can do this by going to Developer > Reload Window.