Even I build package.json and run
npm install ./
npm install dependencies of dependencies even it's perfectly equal.
Ex) ExpressJs 4.0.0-rc4 node_modules/cookie === cookie-parser node_modules/cookie
When nodeJs load file, it's cached per file. So, if npm does not share dependencies, same module can be parsed several time and use much more memory.
The reason is for project maintenance and consistent (and simple) usage. Now each package is perfectly independent of the others, and each package adheres to its own package.json
file.
If one package updates its dependencies, NPM simply has to check that package. If it were sharing a reference, it would not only need to update the new version for the one that changed, but also switch and re-reference the old version for the other package. Or if a package was deleted that shared a reference, NPM would need to re-check all other packages to see if another was still using it. And other odd use cases when sharing.
Storage is cheap these days and most NPM modules are small in (file) size. Ease of maintenance and consistent updating is worth more than saving a few MBs of files.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With