4

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?

asked Oct 1, 2020 at 15:51

1 Answer 1

3

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]
answered Oct 1, 2020 at 16:23
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the confirmation, I was expecting it to work this way only.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.