2

Suppose I have an xyz library (which i installed using npm install xyz --save) and project abc

How does node module determine if the dependency should be in

abc/node_modules or abc/node_modules/xyz/node_modules?

5
  • packages contain a package.json that contains the dependecies, so in your example, abc/node_modules/xyz/package.json contains the dependecies, that nodejs installs with the dependecies of the dependecy and so on until there's no more dependencies missing Commented Jun 3, 2021 at 12:18
  • so are you saying abc/node_modules/xyz/node_modules will contain all the dependency of xyz? if yes? then no, that's not true. like try it on your end? Commented Jun 3, 2021 at 12:20
  • depends on the configuration on that package, but usually if the version is different than the one already installed, it will store the package on xyz/node_module, if it's the same version, it would just use the one on the main root/node_modules/ folder Commented Jun 3, 2021 at 12:24
  • *probably still not true. I made a lib just for the sake of experiment and I noticed it to be in abc/node_modules instead of abc/node_modules/xyz/node_modules Commented Jun 3, 2021 at 12:26
  • it uses the main forlder (root/abc) if the lib doesn't exist, if it does, it uses the one installed only if the version is the same, if not the same version, the package will let the one on the root/node_modules untouched and store it on node_modules/xyz/node_modules. Try creating a dependeancy of a lib that already exists in the root/node_modules folder instead Commented Jun 3, 2021 at 12:29

1 Answer 1

4

Module is placed in nested node_modules if there's a non-compatible version in the "root" node_modules.

What iRohitBhatia is saying in the comments to your question, is partially true. But the important factor is not the version alone - but the version compatibility.


Example:

[email protected] is dependent on follow-redirects@^1.14.0

If you define your package.json like this

{ "dependencies": { "axios": "0.21.1" } } 

it installs both on the same level.

ls node_modules axios follow-redirects 
ls node_modules/axios # there is no nested `node_modules` 

But if you have a non-compatible version of follow-redirects in your node_modules, it installs the compatible version to the nested node_modules/axios/node_modules.

{ "dependencies": { "follow-redirects": "1.0.0", "axios": "0.21.1" } } 
ls node_modules axios debug follow-redirects # the non-compatible `1.0.0` version ms 
ls node_modules/axios/node_modules follow-redirects # the compatible version 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.