--experimental-default-type=type


稳定性: 1.0 - 早期开发

为以下内容定义使用哪种模块系统,modulecommonjs:

🌐 Define which module system, module or commonjs, to use for the following:

  • 如果未指定 --input-type,则通过 --eval 或 STDIN 提供字符串输入。
  • .js 结尾的文件或没有扩展名的文件,如果在同一文件夹或任何父文件夹中没有 package.json 文件存在。
  • .js 结尾的文件或没有扩展名的文件,如果最近的父级 package.json 文件缺少 "type" 字段;除非 package.json 所在文件夹或任何父级文件夹位于 node_modules 文件夹内。

换句话说,--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.