tl;dr
(tl; dr)
Latest NodeJS still lists ES Modules as experimental, behind a flag.
(最新的NodeJS仍将ES模块列为试验性的标志。)
Those looking for a solution to the problem may want to try the esm module loader, which is a production-ready implementation of the ES Modules Spec for NodeJS:
(那些寻求解决问题的人可能想要尝试esm模块加载器,这是NodeJS ES模块规范的生产就绪型实现:)
node -r esm main.js
Detailed Updates...
(详细更新...)
23 April 2019
(2019年4月23日)
A PR recently landed to change the way ES Modules are detected: https://github.com/nodejs/node/pull/26745
(最近降落的PR更改了检测ES模块的方式: https : //github.com/nodejs/node/pull/26745)
It's still behind the --experimental-modules
flag, but there are major changes in the way modules can be loaded:
(它仍然位于--experimental-modules
标志后面,但是模块的加载方式发生了重大变化:)
-
package.type
which can be either module
or commonjs
(package.type
可以是module
或commonjs
)
-
type: "commonjs"
:(type: "commonjs"
:)
-
.js
is parsed as commonjs(.js
被解析为commonjs)
- default for entry point without an extension is commonjs
(不带扩展名的入口点的默认值为commonjs)
-
type: "module":
-
.js
is parsed as esm(.js
被解析为esm)
- does not support loading JSON or Native Module by default
(默认不支持加载JSON或本机模块)
- default for entry point without an extension is esm
(不带扩展名的入口点的默认值为esm)
-
--type=[mode]
to let you set the type on entry point.(--type=[mode]
可让您在入口点设置类型。)
Will override package.type
for entry point.(将覆盖package.type
作为入口点。)
- A new file extension
.cjs
.(新的文件扩展名.cjs
。)
-
--es-module-specifier-resolution=[type]
- options are
explicit
(default) and node
(选项是explicit
(默认)和node
)
- by default our loader will not allow for optional extensions in the import, the path for a module must include the extension if there is one
(默认情况下,我们的加载程序不允许导入中的可选扩展名,如果存在一个扩展名,则模块路径必须包含扩展名)
- by default our loader will not allow for importing directories that have an index file
(默认情况下,我们的加载器不允许导入具有索引文件的目录)
- developers can use
--es-module-specifier-resolution=node
to enable the commonjs specifier resolution algorithm(开发人员可以使用--es-module-specifier-resolution=node
来启用commonjs指定者解析算法)
- This is not a “feature” but rather an implementation for experimentation.
(这不是“功能”,而是实验的一种实现。)
It is expected to change before the flag is removed(预期在删除标志之前会发生变化)
-
--experimental-json-loader
- You can use
package.main
to set an entry point for a module(您可以使用package.main
设置模块的入口点)
17 January 2019
(2019年1月17日)
Node 11.6.0 still lists ES Modules as experimental, behind a flag.
(节点11.6.0仍在标志后面将ES模块列为实验模块。)
13 September 2017
(2017年9月13日)
NodeJS 8.5.0 has been released with support for mjs files behind a flag:
(NodeJS 8.5.0已发布,在标志后面支持mjs文件:)
node --experimental-modules index.mjs
The plan for this is to remove the flag for the v10.0 LTS release.
(计划是删除v10.0 LTS版本的标志。)
--Outdated Information.
(-过时的信息。)
Kept here for historical purposes--(出于历史目的保留在这里)
8 September 2017
(2017年9月8日)
NodeJS master branch has been updated with initial support for ESM modules:
(NodeJS master分支已更新,最初支持ESM模块:)
https://github.com/nodejs/node/commit/c8a389e19f172edbada83f59944cad7cc802d9d5(https://github.com/nodejs/node/commit/c8a389e19f172edbada83f59944cad7cc802d9d5)
This should be available in the latest nightly (this can be installed via nvm to run alongside your existing install):
(它应该在最新的每晚可用(可以通过nvm安装以与您现有的安装一起运行):)
https://nodejs.org/download/nightly/(https://nodejs.org/download/nightly/)
And enabled behind the --experimental-modules
flag:
(并在--experimental-modules
标志后面启用:)
package.json
(package.json)
{
"name": "testing-mjs",
"version": "1.0.0",
"description": "",
"main": "index.mjs" <-- Set this to be an mjs file
}
Then run:
(然后运行:)
node --experimental-modules .
February 2017:
(2017年2月:)
https://medium.com/@jasnell/an-update-on-es6-modules-in-node-js-42c958b890c#.6ye7mtn37
(https://medium.com/@jasnell/an-update-on-es6-modules-in-node-js-42c958b890c#.6ye7mtn37)
The NodeJS guys have decided that the least bad solution is to use the .mjs
file extension.
(NodeJS伙计们认为, 最糟糕的解决方案是使用.mjs
文件扩展名。)
The takeaway from this is:(得出的结论是:)
In other words, given two files foo.js
and bar.mjs
, using import * from 'foo'
will treat foo.js
as CommonJS while import * from 'bar'
will treat bar.mjs
as an ES6 Module
(换句话说,给定两个文件foo.js
和bar.mjs
,使用import * from 'foo'
foo.js
import * from 'foo'
将foo.js
视为CommonJS,而import * from 'bar'
bar.mjs
import * from 'bar'
将bar.mjs
视为ES6模块)
And as for timelines...
(至于时间表...)
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.
(在当前时间点,在Node.js甚至开始研究可支持的ES6模块实现之前,ES6和虚拟机方面仍然需要解决许多规范和实现问题。)
Work is in progress but it is going to take some time — We're currently looking at around a year at least .(工作正在进行中,但将需要一些时间-我们目前至少需要一年左右的时间。)
October 2016:
(2016年10月:)
One of the developers on Node.JS recently attended a