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?
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
package.jsonthat contains the dependecies, so in your example,abc/node_modules/xyz/package.jsoncontains the dependecies, that nodejs installs with the dependecies of the dependecy and so on until there's no more dependencies missingabc/node_modules/xyz/node_moduleswill contain all the dependency of xyz? if yes? then no, that's not true. like try it on your end?