--experimental-config-file=config
如果存在,Node.js 将在指定路径查找配置文件。Node.js 将读取该配置文件并应用设置。配置文件应为具有以下结构的 JSON 文件。在 $schema 中的 vX.Y.Z 必须替换为你正在使用的 Node.js 版本。
🌐 If present, Node.js will look for a configuration file at the specified path.
Node.js will read the configuration file and apply the settings. The
configuration file should be a JSON file with the following structure. vX.Y.Z
in the $schema must be replaced with the version of Node.js you are using.
{
"$schema": "https://nodejs.cn/dist/vX.Y.Z/docs/node-config-schema.json",
"nodeOptions": {
"import": [
"amaro/strip"
],
"watch-path": "src",
"watch-preserve-output": true
},
"testRunner": {
"test-isolation": "process"
}
} 配置文件支持特定于命名空间的选项:
🌐 The configuration file supports namespace-specific options:
nodeOptions字段包含在NODE_OPTIONS中允许的 CLI 标志。- 像
testRunner这样的命名空间字段包含特定于该子系统的配置。
不支持无操作标志。当前并非所有 V8 标志都被支持。
🌐 No-op flags are not supported. Not all V8 flags are currently supported.
可以使用官方 JSON 模式来验证配置文件,这可能会根据 Node.js 版本的不同而有所变化。配置文件中的每个键都对应一个可以作为命令行参数传递的标志。该键的值就是将传递给该标志的值。
🌐 It is possible to use the official JSON schema to validate the configuration file, which may vary depending on the Node.js version. Each key in the configuration file corresponds to a flag that can be passed as a command-line argument. The value of the key is the value that would be passed to the flag.
例如,上面的配置文件等同于以下命令行参数:
🌐 For example, the configuration file above is equivalent to the following command-line arguments:
node --import amaro/strip --watch-path=src --watch-preserve-output --test-isolation=process 配置中的优先级如下:
🌐 The priority in configuration is as follows:
- NODE_OPTIONS 和命令行选项
- 配置文件
- Dotenv NODE_OPTIONS
配置文件中的值不会覆盖环境变量和命令行选项中的值,但会覆盖通过 --env-file 标志解析的 NODE_OPTIONS 环境文件中的值。
🌐 Values in the configuration file will not override the values in the environment
variables and command-line options, but will override the values in the NODE_OPTIONS
env file parsed by the --env-file flag.
键不能在相同或不同的命名空间内重复。
🌐 Keys cannot be duplicated within the same or different namespaces.
如果配置文件包含未知的键或不能在命名空间中使用的键,配置解析器将抛出错误。
🌐 The configuration parser will throw an error if the configuration file contains unknown keys or keys that cannot be used in a namespace.
Node.js 不会对用户提供的配置进行清理或验证, 所以 绝对不要 使用不可信的配置文件。
🌐 Node.js will not sanitize or perform validation on the user-provided configuration, so NEVER use untrusted configuration files.