--input-type=type


这将 Node.js 配置为将 --evalSTDIN 输入解释为 CommonJS 或 ES 模块。有效值为 "commonjs""module""module-typescript""commonjs-typescript""-typescript" 值不适用于标志 --no-experimental-strip-types。默认为 "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 "commonjs".

如果未提供 --input-type,Node.js 将尝试通过以下步骤检测语法:

¥If --input-type is not provided, Node.js will try to detect the syntax with the following steps:

  1. 将输入作为 CommonJS 运行。

    ¥Run the input as CommonJS.

  2. 如果步骤 1 失败,则将输入作为 ES 模块运行。

    ¥If step 1 fails, run the input as an ES module.

  3. 如果步骤 2 因 SyntaxError 而失败,则删除类型。

    ¥If step 2 fails with a SyntaxError, strip the types.

  4. 如果步骤 3 失败并出现错误代码 ERR_UNSUPPORTED_TYPESCRIPT_SYNTAXERR_INVALID_TYPESCRIPT_SYNTAX,则抛出步骤 2 中的错误,包括消息中的 TypeScript 错误,否则以 CommonJS 形式运行。

    ¥If step 3 fails with an error code ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX or ERR_INVALID_TYPESCRIPT_SYNTAX, throw the error from step 2, including the TypeScript error in the message, else run as CommonJS.

  5. 如果步骤 4 失败,则将输入作为 ES 模块运行。

    ¥If step 4 fails, run the input as an ES module.

为了避免多次语法检测延迟,可以使用 --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 不支持此选项。将 --input-type=module--print 一起使用会引发错误,因为 --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.