Configure NPM package to use parent dependency?

Viewed 56

I have been searching a while for this, and maybe I am misunderstanding how NPM package dependencies work, but I am wondering:

If I know a project has both a specific package ("Package A"), as well as my package ("Package B"), as dependencies, and my package also uses Package A, can I configure Package B to use the version of Package A installed to the parent project's node_modules instead of installing it as a dependency under Package B's node_modules?

Example of Package B using Package A's depedency in the main project's node_modules folder

My need for this is rooted in needing to generate correct paths. My package (B) is relying on another package (A) to generate some paths to be used in the parent project, which also lists Package A as a dependency. However, because Package A is generating paths based on its location (a sub-dependency of Package B), the resulting paths are incorrect. To help visualize this:

  • Path I want generated: app/node_modules/package_a/foo
  • Path that gets generated: app/node_modules/package_b/node_modules/package_a/foo

I initially thought that peerDependencies solved this, but it still seemed to rely on the Package A found in Package B's node_modules*, and not with the parent project. If I manually removed Package A from that location, it didn't fall back to the Package A found in the parent project either.

* That's if Package A is even installed; with npm peer dependencies are installed it seems, but with yarn they are not.

Is there something I'm missing with Peer Dependencies, or is this possible another way?

0 Answers
Related