语法检测


【Syntax detection】

稳定性: 1.2 - 发布候选版

Node.js 将检查模糊输入的源代码,以确定其是否包含 ES 模块语法;如果检测到此类语法,该输入将被视为 ES 模块。

【Node.js will inspect the source code of ambiguous input to determine whether it contains ES module syntax; if such syntax is detected, the input will be treated as an ES module.】

不明确的输入定义为:

【Ambiguous input is defined as:】

  • 扩展名为 .js 的文件或没有扩展名的文件;并且没有控制性的 package.json 文件,或者有但缺少 type 字段;并且未指定 --experimental-default-type
  • 当未指定 --input-type--experimental-default-type 时,字符串输入(--eval 或 STDIN)。

ES 模块语法被定义为在作为 CommonJS 评估时会抛出错误的语法。这包括以下内容:

【ES module syntax is defined as syntax that would throw when evaluated as CommonJS. This includes the following:】

  • import 语句(但不包括 import() 表达式,后者在 CommonJS 中是有效的)。
  • export 语句。
  • import.meta 引用。
  • 在模块的顶层使用 await
  • CommonJS 封装器变量的词法重新声明(requiremoduleexports__dirname__filename)。