启用
【Enabling】
Node.js 有两种模块系统:CommonJS 模块和 ECMAScript 模块。
【Node.js has two module systems: CommonJS modules and ECMAScript modules.】
作者可以通过 .mjs 文件扩展名、package.json 中 "type" 字段的值为 "module"、--input-type 标志的值为 "module" 或 --experimental-default-type 标志的值为 "module" 来告诉 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",
the --input-type flag with a value of "module", or the
--experimental-default-type flag with a value of "module". These are
explicit markers of code being intended to run as an ES module.】
反过来,作者Node.js可以通过
'.cjs' 文件扩展名,'package.json' "type" 字段,取值为
“commonjs”,即值为“commonjs”的--input-type标志,或
--experimental-default-type 标志,值为“commonjs”。
【Inversely, authors can tell Node.js to interpret JavaScript as CommonJS via the
.cjs file extension, the package.json "type" field with a value
"commonjs", the --input-type flag with a value of "commonjs", or the
--experimental-default-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.】