--input-type=type
这将配置 Node.js 将 --eval 或 STDIN 输入解释为 CommonJS 或 ES 模块。有效值为 "commonjs"、"module"、"module-typescript" 和 "commonjs-typescript"。带有 "-typescript" 的值在使用 --no-experimental-strip-types 标志时不可用。默认情况下没有值,或者如果传递了 --no-experimental-detect-module,则默认为 "commonjs"。
【This configures Node.js to interpret --eval or STDIN input as CommonJS or
as an ES module. Valid values are "commonjs", "module", "module-typescript" and "commonjs-typescript".
The "-typescript" values are not available with the flag --no-experimental-strip-types.
The default is no value, or "commonjs" if --no-experimental-detect-module is passed.】
如果未提供 --input-type,Node.js 将尝试通过以下步骤来检测语法:
【If --input-type is not provided,
Node.js will try to detect the syntax with the following steps:】
- 以 CommonJS 方式运行输入。
- 如果步骤 1 失败,请将输入作为 ES 模块运行。
- 如果步骤 2 由于语法错误失败,请去掉类型。
- 如果步骤 3 出现错误代码
ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX或ERR_INVALID_TYPESCRIPT_SYNTAX,则抛出步骤 2 的错误,并在消息中包含 TypeScript 错误,否则以 CommonJS 方式运行。 - 如果第4步失败,请将输入作为ES模块运行。
为了避免多次语法检测带来的延迟,可以使用 --input-type=type 标志来指定 --eval 输入的解释方式。
【To avoid the delay of multiple syntax detection passes, the --input-type=type flag can be used to specify
how the --eval input should be interpreted.】
REPL 不支持此选项。在 --print 中使用 --input-type=module 会导致错误,因为 --print 不支持 ES 模块语法。
【The REPL does not support this option. Usage of --input-type=module with
--print will throw an error, as --print does not support ES module
syntax.】