--experimental-config-file=config


稳定性: 1.0 - 早期开发

¥Stability: 1.0 - Early development

如果存在,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 标志。

    ¥The nodeOptions field contains CLI flags that are allowed in NODE_OPTIONS.

  • testRunner 这样的命名空间字段包含特定于该子系统的配置。

    ¥Namespace fields like testRunner contain configuration specific to that subsystem.

不支持无操作标志。目前并非所有 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:

  1. NODE_OPTIONS 和命令行选项

    ¥NODE_OPTIONS and command-line options

  2. 配置文件

    ¥Configuration file

  3. 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.