npm package.json dependencies - for a library component

71 Views Asked by At

Lets say I am working on a library that will be consumed by other developers. MyPackage has a dependency on moment. The developer that consumes my package also has a dependency on moment. So moment will exist as a "dependency" in both library package.json and application package.json (and thus get packaged twice). Is there a way to package it just once? If the consumer has it, use theirs, else use mine?

1

There are 1 best solutions below

0
On

It's already happening by default on fresh installs if dependency ranges match.

npm v>=3 does gang the dependencies, depending on the installation order and depth, see here.

Also, if you kept working on the same folder for a while, there might be some cruft, which could be wiped using npm dedupe, see here.

In theory, moment should not be duplicated if both your library and developer's library are consuming the same version ranges of it. At least if npm dedupe is called or node_modules are wiped and npm i-nstalled.