package.json 中的 phantomChildren 是什么?

What are phantomChildren in package.json?

当我安装模块时,phantomChildren 的列表出现在 package.json 文件中。什么是 phantomChildren

我没有找到 npm 软件包 phantomChildren 的官方文档。但是遇到了一些其他的解释:https://rushjs.io/pages/advanced/phantom_deps/。它是关于 rast,但很好地解释了 npm 依赖项的行为。

例如,库 A 可能会从库 BC 导入定义,但是 BC 都可以从 [=20 导入=],这在这四个包之间创建了“钻石依赖关系”。

A “phantom dependency” occurs when a project uses a package that is not defined in its package.json file.

一些活生生的例子:

my-library/package.json

{
  "name": "my-library",
  "version": "1.0.0",
  "main": "lib/index.js",
  "dependencies": {
    "minimatch": "^3.0.4"
  },
  "devDependencies": {
    "rimraf": "^2.6.2"
  }
}

my-library/lib/index.js

var minimatch = require("minimatch")
var expand = require("brace-expansion");  // ???
var glob = require("glob")  // ???

Wait a sec – two of these libraries are not declared as dependencies in the package.json file. How is this working at all!? It turns out that brace-expansion is a dependency of minimatch, and glob is a dependency of rimraf. During installation, NPM has flattened their folders to be under my-library/node_modules. The NodeJS require() function finds them there because it probes for folders without considering the package.json files at all.

总结一下: 如果包使用它自己的依赖项的依赖项,它可以被视为 phantomChildren。包不直接有这样的依赖关系,而是从其他地方使用它。