简体   繁体   中英

force different npm modules to share same dependency

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM