Reputation: 105
Sorry for the NOOB question, I am new to npm and front end development.
I have a library (A) which needs to be a singleton but has an init to pass start up config. I also have a library B which has library A as a dependency. In my web client I need to reference library A directly and also library B. I init library A in my web client.
From my investigation it looks like my web client and library B are using there own copy of library A and therefore because library A only works as a singleton, library B's call to library A is failing with undefined
.
library A
library B
|----library A
web-client
|----library A
|----library B
|----library A
In my web client library A and B are referenced in package.json. My question is is there a way to tell library B to use the library A the web-client has referenced directly?
Upvotes: 5
Views: 1373
Reputation: 222369
This is supported in NPM 3 and higher.
One copy of library A
will be installed and will be used by both library B
and web-client
, as long as library A
version constraints match in library B
and web-client
dependencies.
If version constraints don't match, library B
and web-client
will have their own copies of library A
.
It's possible to get multiple copies of library A
if project dependencies weren't installed simultaneously.
In this case
npm dedupe
or removing node_modules
and reinstalling dependencies with
npm i
will help.
Upvotes: 2