Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid same node.js packages installing several times when they are used as dependencies by different modules?

Tags:

node.js

npm

I noticed that npm installs the same node.js packages several times when they are required by different modules.

Is there an option to switch this off and to have one repo for all the packages?

I looked but did not find anything on this...

like image 342
Aerodynamika Avatar asked Jan 26 '23 14:01

Aerodynamika


1 Answers

This is the default behaviour of NPM since version 3. Quoting the changelog of that version:

Your dependencies will now be installed maximally flat. Insofar as is possible, all of your dependencies, and their dependencies, and THEIR dependencies will be installed in your project's node_modules folder with no nesting. You'll only see modules nested underneath one another when two (or more) modules have conflicting dependencies.

As you can see, if you have modules which have conflicting dependencies (i.e. require different versions of the same dependency), multiple versions of such dependencies will be installed, and this cannot be avoided.

If you suspect that your node_modules directory somehow contains duplicates, you can use the command npm dedupe (docs) to find these and reduce the duplication, leaving only one instance of each duplicate.

like image 53
Mikhail Burshteyn Avatar answered May 20 '23 07:05

Mikhail Burshteyn