Woody
Woody

Reputation: 8022

NPM local package install

This might well be an ill conceived idea, but I have two react projects in version control, the first, let's call it A, contains a component I want to use in B. B therefore has a dependency on A declared in the package.json for B, as a file: ...path to project A.

The problem is that in order to build project B, the user needs to load both A and B onto their disk, then build A (this is a rollup build) and then build B. Because (I think) B depends on A with a file: reference, when installing A, NPM copies the whole directory including the node_modules folder of A under B. So you end up with B/node_modules/A/node_modules

I think the issue is that we are using the file system location of project A as both the source code location and the registry location if that makes sense. Perhaps we need to publish project A somewhere when it gets built and declare that location to be the dependency?

I hope that makes sense.

I looked at the docs and it seems like node_modules should always be ignored on an install if I am understanding the files section at all.

Upvotes: 0

Views: 394

Answers (1)

Matus Dubrava
Matus Dubrava

Reputation: 14502

Common pattern for reusing components between projects is to move the component (or any piece of code that needs to be reused) outside of either A and B and create a standalone npm module containing that code/components.

Publish that module to either public or private npm repository depending on the sensitivity of the code and let both projects A and B install its own instance of that npm module. Otherwise these interdependencies will only cause you headaches in a long run.

Upvotes: 1

Related Questions