Reputation: 783
I have 3 node.js modules, A
, B
and C
. All of them private git repos. A
depends on B
depends on C
. Git cloning A
and doing npm install
works like a charm.
But while coding on module A
, i want to work on B
(and C
) as well. The latter two are git cloned too. And npm link ../pathto/B
works well.
And as B
depends on C
, npm link
took care of "installing" C
into B/node_modules/C
. Its a static file clone, that's being used by B
.
So when doing npm link ../pathto/C
, it results in A/node_modules/C
(being a symbolic link).
But, and thats the problem, B
will use its static clone of C
, rather than what i have linked into A/node_modules/C
.
A/
...
node_modules/
B -> B/
C -> C/
B/
...
node_modules/
C/
...
C/
...
Does anyone have an idea to work around this?
Upvotes: 26
Views: 17293
Reputation: 1397
It's also possible to provide the file paths without a prior 'npm link':
e.g. npm link ../A ../B ../../somefolder/C
Upvotes: 1
Reputation: 10075
For me, a simple
npm link A B C
works. Using npm 7.5.2
Of course, the packages have to be exposed prior, so
sudo npm link
must be run within each package's directory.
This does require a bit of gymnastics with package.json
: during installations of new packages, dependencies on local modules must be hidden from requirements (or probably specified as paths, but I don't have sufficient knowledge for that yet). After that, the local packages need to be linked again. They must also ALWAYS be linked using one single command or npm will simply remove the missed ones.
Update 2021-11-08:
I'm using Vue and the above solution resulted in multiple Vue instances in my builds. It seems each of the linked modules disregarded the "normal" way of importing modules and did their own imports separately.
So now I'm using npm pack
in the libraries and specifying dependencies in using projects as file dependencies "my_package_name": "file:/tmp/my_package_name-x.y.z.tgz",
Upvotes: 35
Reputation: 783
I solved it, or at least i got it working.
After npm install
i do npm link _node_modules/*
(_node_modules is the directory where my local modules B
and C
reside in).
So far B
gets required as planned. But still B
loads its static C
reference.
Then i simply cd
to _node_modules/B/node_modules
and perform npm link C
.
Upvotes: 5