为什么安装一个 npm 包会在 node_modules 中添加许多子文件夹?

Why does installing one npm package add many subfolders in node_modules?

我更新了我在 grunt 项目中使用的节点/npm。

然而,当我现在想将一个包添加到项目 myproject/(比如 npm install grunt-favicon)时,它似乎将数百个文件夹添加到 myproject/node_modules/,而它过去只添加一个- 通常以 grunt 作为前缀,例如 node_modules/grunt-favicon.

我对这些东西的了解是基本的,但这些似乎是必需的依赖项,甚至其中一些项目有更多的依赖项。

不应该在其他地方添加这些依赖项吗?如果是这样我该如何纠正?

我使用 npm 3.3.5node 4.1.1grunt-cli v0.1.13grunt v0.4.5

并且此屏幕截图在左侧显示 ~/.npm,然后在右侧显示 ~/myproject/node_modules

这是正确的,别担心。您仍在本地安装软件包,只是目录结构看起来有点不同。这是 在 npm v3.0.0 中更改的行为:现在所有依赖项的依赖项都直接安装在 node_modules 文件夹中(只要这不会导致版本冲突).这大大减少了文件树的大小。

来自npm 3.0.0 release notes

Flat, flat, flat!

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.