为什么NPM模块的依赖关系始终可以安装到嵌套的node_modules?

如您所知,NPM v3尝试使依赖树尽可能保持平坦.以前我认为它只是嵌套安装依赖项来解决冲突,以防同一个模块有多个版本.

但是,我注意到模块量角器有些奇怪.它有一个依赖项,webdriver-manager,它总是作为嵌套安装到node_modules / protractor / node_modules.您可以通过在空文件夹中运行npm install protractor轻松重现此问题.

为什么会这样?

最佳答案 这是一个很好的问题,目前的行为是预期的. npmV3确实在平面结构中安装了依赖项,而Protractor也以相同的方式安装

使用npmV3安装Protractor时

《为什么NPM模块的依赖关系始终可以安装到嵌套的node_modules?》

当使用npmV2安装Protractor时.*

《为什么NPM模块的依赖关系始终可以安装到嵌套的node_modules?》

所有模块都安装在Flat Structure中,但仍然是webdriver-manager仍然是嵌套结构.这是因为存在冲突的依赖关系.

As per official documentation

Your dependencies will now be installed maximally flat. Insofar as is
possible, all of your dependencies, and their dependencies, and THEIR
dependencies will be installed in your project’s node_modules folder
with no nesting. You’ll only see modules nested underneath one another
when two (or more) modules have conflicting dependencies.

Now, let’s say we want to require another module, C. C requires B, but
at another version than A.However, since B v1.0 is already a top-level
dep, we cannot install B v2.0 as a top level dependency. npm v3
handles this by defaulting to npm v2 behavior and nesting the new,
different, module B version dependency under the module that requires
it — in this case, module C.

webdriver-manager package.json列出了依赖项“minimist”:“^ 1.2.0”,这是其他软件包的冲突要求,比如乐观主义者需要以下依赖项列表

  ├─ optimist@0.6.1
   │  ├─ wordwrap@0.0.3
   │  └─ minimist@0.0.10

因此,由于依赖性冲突,webdriver-manager安装在量角器node_modules中

执行npm-remote-ls量角器以获取完整的依赖关系树以及所有依赖关系中的冲突

点赞