--experimental-config-file=config


稳定性: 1.0 - 早期开发

如果存在,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
  },
  "test": {
    "test-isolation": "process"
  },
  "watch": {
    "watch-preserve-output": true
  }
} 

配置文件支持特定于命名空间的选项:

🌐 The configuration file supports namespace-specific options:

  • nodeOptions 字段包含在 NODE_OPTIONS 中允许的 CLI 标志。
  • 命名空间字段如 testwatchpermission 包含该子系统特定的配置。

当配置文件中存在命名空间时,Node.js 会自动启用相应的标志(例如,--test--watch--permission)。这允许你配置特定子系统的选项,而无需在命令行上显式传递标志。

🌐 When a namespace is present in the configuration file, Node.js automatically enables the corresponding flag (e.g., --test, --watch, --permission). This allows you to configure subsystem-specific options without explicitly passing the flag on the command line.

例如:

🌐 For example:

{
  "test": {
    "test-isolation": "process"
  }
} 

等同于:

🌐 is equivalent to:

node --test --test-isolation=process 

要在仍使用命名空间选项的情况下禁用自动标志,你可以在命名空间中显式将标志设置为 false

🌐 To disable the automatic flag while still using namespace options, you can explicitly set the flag to false within the namespace:

{
  "test": {
    "test": false,
    "test-isolation": "process"
  }
} 

不支持无操作标志。当前并非所有 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 和命令行选项
  2. 配置文件
  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.