Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does NPM try to install same dependent package multiple times or only once?

Would like to understand, how npm handles installation of duplicate packages with same version.

  1. Does it ignore, if the same package - same version installed already?
  2. Does it install/overwrite again the same package?

For eg:

  1. Package A is dependent on package B and package C.
  2. Where Packages B and C are dependent on common package D version 1.0

so while running npm install will it try to install package D twice or only once and will ignore second occurrence as its already present in node_modules?

like image 710
Rohit Singh Avatar asked Nov 16 '25 00:11

Rohit Singh


1 Answers

The NPM install algorithm states dependencies will be added as close to the top as is possible without breaking any other modules.

In your case, package B and C both depend on [email protected]. Since there is no possible conflict between the two dependencies, package D will be installed once at the top level.

For a rough representation, here is the original dependency tree you have imagined.

+ package A
+-- B
   -- [email protected]
+-- C
   -- [email protected]

Package A installs dep B and dep C at the same top level. Since there are no other conflicting versions of dep D in the tree, dep D also gets added to the top level for both dep B and dep C to use.

+ package A
+-- B
+-- C
+-- [email protected]
like image 55
saulmaldonado Avatar answered Nov 17 '25 19:11

saulmaldonado