新标志--experimental-detect-module
可用于自动运行ES
模块的语法可以被检测到。对于“模棱两可”的文件.js
或无扩展名的文件,没有package.json
,带有type
字段,Node.js
将解析文件以检测ES
模块语法;如果找到将运行文件作为ES
模块,否则它将作为CommonJS
模块运行文件。这同样适用于通过--eval
或STDIN
的字符串输入。
我们希望在Node.js
的未来版本中默认启用检测。检测会增加启动时间,因此我们鼓励每个人将type
字段添加到package.json
,即使是默认的"type": "commonjs"
. type
字段或显式扩展的存在例如.mjs
或.cjs
,将选择退出检测。
The new flag --experimental-detect-module can be used to automatically run ES modules when their syntax can be detected. For “ambiguous” files, which are .js or extensionless files with no package.json with a type field, Node.js will parse the file to detect ES module syntax; if found, it will run the file as an ES module, otherwise it will run the file as a CommonJS module. The same applies to string input via --eval or STDIN.
We hope to make detection enabled by default in a future version of Node.js. Detection increases startup time, so we encourage everyone — especially package authors — to add a type field to package.json, even for the default "type": "commonjs". The presence of a type field, or explicit extensions such as .mjs or .cjs, will opt out of detection.
https://github.com/nodejs/node/releases/tag/v21.1.0
关注公众号👇👇👇最后不要忘了点个赞再走噢