--experimental-default-type=type
¥Stability: 1.0 - Early development
定义用于以下目的的模块系统 module 或 commonjs:
¥Define which module system, module or commonjs, to use for the following:
-
如果未指定
--input-type,则通过--eval或 STDIN 提供字符串输入。¥String input provided via
--evalor STDIN, if--input-typeis unspecified. -
如果同一文件夹或任何父文件夹中不存在
package.json文件,则以.js结尾或没有扩展名的文件。¥Files ending in
.jsor with no extension, if there is nopackage.jsonfile present in the same folder or any parent folder. -
如果最近的父
package.json字段缺少"type"字段,则以.js结尾或没有扩展名的文件;除非package.json文件夹或任何父文件夹位于node_modules文件夹内。¥Files ending in
.jsor with no extension, if the nearest parentpackage.jsonfield lacks a"type"field; unless thepackage.jsonfolder or any parent folder is inside anode_modulesfolder.
换句话说,--experimental-default-type=module 将 Node.js 当前默认为 CommonJS 的所有位置翻转为默认为 ECMAScript 模块(node_modules 以下的文件夹和子文件夹除外),以实现向后兼容性。
¥In other words, --experimental-default-type=module flips all the places where
Node.js currently defaults to CommonJS to instead default to ECMAScript modules,
with the exception of folders and subfolders below node_modules, for backward
compatibility.
在 --experimental-default-type=module 和 --experimental-wasm-modules 下,没有扩展名的文件如果以 WebAssembly 幻数 (\0asm) 开头,将被视为 WebAssembly;否则它们将被视为 ES 模块 JavaScript。
¥Under --experimental-default-type=module and --experimental-wasm-modules,
files with no extension will be treated as WebAssembly if they begin with the
WebAssembly magic number (\0asm); otherwise they will be treated as ES module
JavaScript.