介绍
¥Introduction
当传递给 node
作为初始输入时,或者当被 import
语句或 import()
表达式引用时,Node.js 会将以下内容视为 ES 模块:
¥Node.js will treat the following as ES modules when passed to node
as the
initial input, or when referenced by import
statements or import()
expressions:
-
扩展名为
.mjs
的文件。¥Files with an
.mjs
extension. -
当最近的父
package.json
文件包含值为"module"
的顶层"type"
字段时,扩展名为.js
的文件。¥Files with a
.js
extension when the nearest parentpackage.json
file contains a top-level"type"
field with a value of"module"
. -
字符串作为参数传入
--eval
,或通过STDIN
管道传输到node
,带有标志--input-type=module
。¥Strings passed in as an argument to
--eval
, or piped tonode
viaSTDIN
, with the flag--input-type=module
.
当传递给 node
作为初始输入时,或者当被 import
语句或 import()
表达式引用时,Node.js 会将以下内容视为 CommonJS:
¥Node.js will treat the following as CommonJS when passed to node
as the
initial input, or when referenced by import
statements or import()
expressions:
-
扩展名为
.cjs
的文件。¥Files with a
.cjs
extension. -
当最近的父
package.json
文件包含值为"commonjs"
的顶层字段"type"
时,则扩展名为.js
的文件。¥Files with a
.js
extension when the nearest parentpackage.json
file contains a top-level field"type"
with a value of"commonjs"
. -
字符串作为参数传入
--eval
或--print
,或通过STDIN
管道传输到node
,带有标志--input-type=commonjs
。¥Strings passed in as an argument to
--eval
or--print
, or piped tonode
viaSTDIN
, with the flag--input-type=commonjs
.
除了这些明确的情况之外,还有其他情况,Node.js 根据 --experimental-default-type
标志的值默认使用一个模块系统或另一个模块系统:
¥Aside from these explicit cases, there are other cases where Node.js defaults to
one module system or the other based on the value of the
--experimental-default-type
flag:
-
如果同一文件夹或任何父文件夹中不存在
package.json
文件,则以.js
结尾或没有扩展名的文件。¥Files ending in
.js
or with no extension, if there is nopackage.json
file present in the same folder or any parent folder. -
如果最近的父
package.json
字段缺少"type"
字段,则以.js
结尾或没有扩展名的文件;除非该文件夹位于node_modules
文件夹内。(当package.json
文件缺少"type"
字段时,无论--experimental-default-type
如何,为了向后兼容,node_modules
下的包范围始终被视为 CommonJS。)¥Files ending in
.js
or with no extension, if the nearest parentpackage.json
field lacks a"type"
field; unless the folder is inside anode_modules
folder. (Package scopes undernode_modules
are always treated as CommonJS when thepackage.json
file lacks a"type"
field, regardless of--experimental-default-type
, for backward compatibility.) -
当未指定
--input-type
时,字符串作为参数传递给--eval
或通过STDIN
通过管道传递给node
。¥Strings passed in as an argument to
--eval
or piped tonode
viaSTDIN
, when--input-type
is unspecified.
该标志当前默认为 "commonjs"
,但将来可能会更改为默认为 "module"
。因此,最好尽可能明确;特别是,包作者应始终在其 package.json
文件中包含 "type"
字段,即使在所有源都是 CommonJS 的包中也是如此。如果 Node.js 的默认类型发生变化,显式说明包的 type
将使包面向未来,它还将使构建工具和加载器更容易确定应如何解释包中的文件。
¥This flag currently defaults to "commonjs"
, but it may change in the future to
default to "module"
. For this reason it is best to be explicit wherever
possible; in particular, package authors should always include the "type"
field in their package.json
files, even in packages where all sources are
CommonJS. Being explicit about the type
of the package will future-proof the
package in case the default type of Node.js ever changes, and it will also make
things easier for build tools and loaders to determine how the files in the
package should be interpreted.