启用
【Enabling】
Node.js 有两种模块系统:CommonJS 模块和 ECMAScript 模块。
【Node.js has two module systems: CommonJS modules and ECMAScript modules.】
作者可以通过 .mjs 文件扩展名、package.json 中值为 "module" 的 "type" 字段,或值为 "module" 的 --input-type 标志来告诉 Node.js 将 JavaScript 解释为 ES 模块。这些都是明确标示代码被用于作为 ES 模块运行的标记。
【Authors can tell Node.js to interpret JavaScript as an ES module via the .mjs
file extension, the package.json "type" field with a value "module",
or the --input-type flag with a value of "module". These are explicit
markers of code being intended to run as an ES module.】
反过来,作者也可以明确告诉Node.js将JavaScript解释为
通过“.cjs”文件扩展名和“package.json”"type"字段访问CommonJS
其值为“commonjs”,或--input-type标志,值为
“普通人”。
【Inversely, authors can explicitly tell Node.js to interpret JavaScript as
CommonJS via the .cjs file extension, the package.json "type" field
with a value "commonjs", or the --input-type flag with a value of
"commonjs".】
当代码缺少显式标记来指示使用哪种模块系统时,Node.js 会检查模块的源代码以查找 ES 模块语法。如果发现此类语法,Node.js 会将代码作为 ES 模块运行;否则,它将作为 CommonJS 模块运行。更多详情请参见 确定模块系统。
【When code lacks explicit markers for either module system, Node.js will inspect the source code of a module to look for ES module syntax. If such syntax is found, Node.js will run the code as an ES module; otherwise it will run the module as CommonJS. See Determining module system for more details.】