我一直在互联网上寻找,但没有明确的答案。
当前 Node.js 仅使用 CommonJS加载模块的语法,如果你真的想使用标准的 ECMAScript 2015 模块语法,你要么必须事先转译它,要么在运行时使用外部模块加载器。
目前我对使用这两种方法中的任何一种都不太乐观,Node.js 维护者是否甚至计划支持 ECMAScript 2015 模块?我还没有找到任何关于这个的提示。
目前,Node.js 6.x 声称支持 96% 的 ECMAScript 2015 功能,但没有任何对模块的引用( Node.js ECMAScript 2015 support link )。
您知道 Node.js 是否会在不久的将来立即支持这些模块?
最佳答案
Node.js 13.2.0 及以上
Node.js 13.2.0现在支持没有标志的 ES 模块 🎉。但是,该实现仍被标记为实验性的,因此在生产中谨慎使用。
要在 13.2.0 中启用 ECMAScript 模块 (ESM) 支持,请将以下内容添加到您的 package.json
:
{
"type": "module"
}
全部 .js
, .mjs
(或没有扩展名的文件)将被视为 ESM。除了整个
package.json
之外,还有许多不同的选项。选择加入,所有这些都在 documentation for 13.2.0 中有详细说明.Node.js 13.1.0 及以下
那些仍在使用旧版本 Node 的人可能想尝试
[esm][3]
模块加载器,它是 Node.js 的 ES 模块规范的生产就绪实现:node -r esm main.js
详细更新... 2019 年 4 月 23 日
最近有一个 PR 改变了检测 ECMAScript 模块的方式:
https://github.com/nodejs/node/pull/26745
它仍然落后于
--experimental-modules
标志,但加载模块的方式有重大变化:package.type
可以是 module
或 commonjs
type: "commonjs"
:.js
被解析为 CommonJS type: "module"
:.js
被解析为 ECMAScript 模块 --type=[mode]
让您在入口点设置类型。将覆盖 package.type
为入口点。 .cjs
.module
中导入 CommonJS模式。 --es-module-specifier-resolution=[type]
explicit
(默认)和 node
--es-module-specifier-resolution=node
启用 CommonJS 说明符解析算法 --experimental-json-loader
"type": "module"
时导入 JSON 的唯一方法import 'thing.json'
将通过独立于模式的实验加载器 package.main
为模块设置入口点2019 年 1 月 17 日
Node.js 11.6.0仍然将 ES 模块列为实验性的,在一个标志后面。
2017 年 9 月 13 日
Node.js 8.5.0已发布,支持标记后面的 mjs 文件:
node --experimental-modules index.mjs
这样做的计划是删除 v10.0 LTS 版本的标志。——过时的信息。留在这里是为了历史目的——
2017 年 9 月 8 日
Node.js 主分支已经更新,初始支持 ESM 模块:
https://github.com/nodejs/node/commit/c8a389e19f172edbada83f59944cad7cc802d9d5
这应该在最新的每晚可用(这可以是 installed via nvm 与您现有的安装一起运行):
https://nodejs.org/download/nightly/
并在
--experimental-modules
后面启用标志:package.json
{
"name": "testing-mjs",
"version": "1.0.0",
"description": "",
"main": "index.mjs" <-- Set this to be an mjs file
}
然后运行:node --experimental-modules .
2017 年 2 月:An Update on ES6 Modules in Node.js
Node.js 人员已经决定,最不坏的解决方案是使用
.mjs
文件扩展名。从中得出的结论是:In other words, given two files
foo.js
andbar.mjs
, usingimport * from 'foo'
will treatfoo.js
as CommonJS whileimport * from 'bar'
will treatbar.mjs
as an ES6 Module
至于时间线...
At the current point in time, there are still a number of specification and implementation issues that need to happen on the ES6 and Virtual Machine side of things before Node.js can even begin working up a supportable implementation of ES6 modules. Work is in progress but it is going to take some time — We’re currently looking at around a year at least.
2016 年 10 月:
Node.js 的一位开发人员最近参加了 TC-39 session ,并写了一篇关于 Node.js 实现的阻塞器的精彩文章:
Node.js, TC-39, and Modules
基本的结论是:
*.mjs
似乎是最可能的解决方案,除非他们可以在没有用户输入的情况下准确检测 ECMAScript 模块 ——原答案——
一段时间以来,这一直是一个烫手山芋。最重要的是,Node.js 最终会支持 ES2015导入/导出模块的语法 - 最有可能发生在 specification for loading modules最终确定并达成一致。
这是a good overview是什么阻碍了 Node.js。本质上,他们需要确保新规范适用于 Node.js,Node.js 主要是有条件的同步加载,也适用于主要是异步的 HTML。
现在没有人确切知道,但我想 Node.js 会支持
import/export
用于静态加载,除了新的 System.import
用于动态加载 - 同时仍保持 require
对于遗留代码。下面是一些关于 Node 如何实现这一目标的建议:
关于javascript - Node.js 计划支持导入/导出 ES6 (ECMAScript 2015) 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37132031/