process.allowedNodeEnvironmentFlags


process.allowedNodeEnvironmentFlags 属性是 NODE_OPTIONS 环境变量中允许的特殊的只读 Set 标志。

¥The process.allowedNodeEnvironmentFlags property is a special, read-only Set of flags allowable within the NODE_OPTIONS environment variable.

process.allowedNodeEnvironmentFlags 继承了 Set,但覆盖了 Set.prototype.has 以识别几种不同的可能标志表示。在以下情况下,process.allowedNodeEnvironmentFlags.has() 将返回 true

¥process.allowedNodeEnvironmentFlags extends Set, but overrides Set.prototype.has to recognize several different possible flag representations. process.allowedNodeEnvironmentFlags.has() will return true in the following cases:

  • 标志可以省略前导单 (-) 或双 (--) 破折号;例如,inspect-brk 代表 --inspect-brkr 代表 -r

    ¥Flags may omit leading single (-) or double (--) dashes; e.g., inspect-brk for --inspect-brk, or r for -r.

  • 传递给 V8 的标志(如 --v8-options 中所列)可以将一个或多个非前导破折号替换为下划线,反之亦然;例如,--perf_basic_prof--perf-basic-prof--perf_basic-prof 等。

    ¥Flags passed through to V8 (as listed in --v8-options) may replace one or more non-leading dashes for an underscore, or vice-versa; e.g., --perf_basic_prof, --perf-basic-prof, --perf_basic-prof, etc.

  • 标志可能包含一个或多个等号 (=) 字符;包括第一个等号在内的所有字符都将被忽略;例如,--stack-trace-limit=100

    ¥Flags may contain one or more equals (=) characters; all characters after and including the first equals will be ignored; e.g., --stack-trace-limit=100.

  • NODE_OPTIONS 中必须允许标志。

    ¥Flags must be allowable within NODE_OPTIONS.

遍历 process.allowedNodeEnvironmentFlags 时,flags 只会出现一次;每个都以一个或多个破折号开头。传给 V8 的标志将包含下划线而不是非前导破折号:

¥When iterating over process.allowedNodeEnvironmentFlags, flags will appear only once; each will begin with one or more dashes. Flags passed through to V8 will contain underscores instead of non-leading dashes:

import { allowedNodeEnvironmentFlags } from 'node:process';

allowedNodeEnvironmentFlags.forEach((flag) => {
  // -r
  // --inspect-brk
  // --abort_on_uncaught_exception
  // ...
});const { allowedNodeEnvironmentFlags } = require('node:process');

allowedNodeEnvironmentFlags.forEach((flag) => {
  // -r
  // --inspect-brk
  // --abort_on_uncaught_exception
  // ...
});

process.allowedNodeEnvironmentFlags 的方法 add()clear()delete() 什么都不做,会静默失败。

¥The methods add(), clear(), and delete() of process.allowedNodeEnvironmentFlags do nothing, and will fail silently.

如果 Node.js 是在没有 NODE_OPTIONS 支持的情况下编译的(如 process.config 所示),process.allowedNodeEnvironmentFlags 将包含允许的内容。

¥If Node.js was compiled without NODE_OPTIONS support (shown in process.config), process.allowedNodeEnvironmentFlags will contain what would have been allowable.