6

I'm new to the node.js world and I was asking how I could handle dependencies versions conflics (which often appears with transitive dependencies): on the internet found only this article useful for me http://nodejs.org/api/modules.html#modules_addenda_package_manager_tips.

So it seems that i don't have to worry about conflicts because of how the packages are managed in node.js. Am I wrong, am I missing something? This seems strange (but still makes sense) to me, I'm used to handle dependencies with maven, setting the transitive dependencies that don't have to be downloaded.

Any help is appreciated, thank you.

reallynice
  • 1,289
  • 2
  • 21
  • 41

1 Answers1

6

npm and the node require system will take care of this for you automatically. For example, your program can depend on dep1 and dep2. dep1 can require subdep version 1.0 and dep2 can require subdep version 2.0, and npm will install multiple versions so each module gets the dependency versions it needs.

your-module/
    node_modules/
        dep1/
            node_modules/
                subdep/ (1.0)
        dep2/
            node_modules/
                sudbep/ (2.0)
Peter Lyons
  • 142,938
  • 30
  • 279
  • 274
  • 2
    Great, thank you: I was mainly looking for an official reference, and then a reassurance that I was right. Following your link I found interesting the section "Why can't npm just put everything in one place, like other package managers?" which pointed in turn to https://npmjs.org/doc/folders.html. Particularly, in the folders docs, I found _the_ answer in the section "examples" of the section "Cycles, Conflicts, and Folder Parsimony". – reallynice May 28 '13 at 08:48
  • Is there a way to prohibit this behaviour? So if there is a conflict. use the lowest number – Daniel Sep 16 '16 at 12:32
  • 2
    No, there is not. Because that would be very likely to crash at runtime. If depA requires subdepFoo@2.0.0 and depB requires subdepFoo@1.5.0 and npm installed just subdefFoo@1.5.0, depA would try to use the new 2.0.0 APIs, which would not exist, and the program would crash with an exception. – Peter Lyons Sep 16 '16 at 14:28
  • If i have had 5 more dependencies dep3 to dep 8, depending on subdep 2.0, will it copy the same subdep 2.0 in every dependencies node_modules? Resulting in 6 copies in total? – Jaydip Kalkani Apr 07 '22 at 03:41