语法检测
【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 封装器变量的词法重新声明(
require、module、exports、__dirname、__filename)。