且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

NodeJS计划支持导入/导出es6(es2015)模块

更新时间:2022-10-27 18:58:18

2017年2月更新: / strong>



https://medium.com/@jasnell/an-update-on-es6-modules-in-node-js-42c958b890c#.6ye7mtn37



NodeJS家伙已经决定,最不好的解决方案是使用 .mjs 文件扩展名。这样做的是:


换句话说,给了两个文件 foo.js bar.mjs ,使用 import *
从'foo'
将处理 foo.js as CommonJS,而 import'from'bar'
将处理 bar.mjs 作为ES6模块


以及时间线...


在目前的时间点上,还有一些
规范和实现问题需要在ES6
和虚拟机端的事情之前发生Node.js甚至可以开始
处理ES6模块的可支持的实现。工作是在
进度,但它将需要一些时间 - 我们目前正在大约一年的
至少


更新2016年10月



Node.JS上的一名开发人员最近参加了TC-39会议并撰写了关于Node.JS实施阻止程序的精辟的文章:



https://hackernoon.com/node-js-tc-39-and-modules-a1118aecf95e



基本的外包是:




  • ES模块被静态分析,CommonJS被评估

  • CommonJS模块允许猴子修补导出,ES模块目前不会

  • 很难检测什么是ES模块,什么是CommonJS没有一些形式的用户输入,但他们正在尝试。

  • *。mjs 似乎是最有可能的解决方案,除非他们可以交流curly检测ES模块,无需用户输入



- 原始答案 -



这是一个很辣的土豆。底线是的,Node将最终支持导入/导出模块的ES2015语法 - 很可能当加载模块的规范定稿并达成一致。



这里是一个很好的概述是什么举行NodeJS。基本上,他们需要确保新的规范适用于主要是有条件的,同步加载的Node以及主要是异步的HTML。



目前没有人知道,但是我想象,除了新的 System.import 之外,Node将支持静态加载 import / export 加载 - 同时仍然保留需要的旧代码。



以下是一些关于Node如何实现这一点的建议: / p>


I've been looking all over the internet without a clear answer for this.

Currently NodeJS uses only CommonJS syntax to load modules, and if you really want to use the standard ES2015 modules syntax, you either have to transpile it beforehand or use an external module loader at runtime.

Currently I'm not too positive to use either of those two methods, are the NodeJS maintainers even planning to support ES2015 modules or not? I haven't found an hint at all about this.

At the moment NodeJS 6.x claims to support 96% of the ES2015 features, but there isn't any reference to modules (NodeJS ES2105 support link).

Do you know if NodeJS will support these modules out of the box, in the near future?

Update February 2017:

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. 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

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. Work is in progress but it is going to take some time — We’re currently looking at around a year at least.

Update October 2016:

One of the developers on Node.JS recently attended a TC-39 meeting and wrote up a superb article on the blockers to implementing for Node.JS:

https://hackernoon.com/node-js-tc-39-and-modules-a1118aecf95e

The basic take-away from that is:

  • ES Modules are statically analyzed, CommonJS are evaluated
  • CommonJS modules allow for monkey-patching exports, ES Modules currently do not
  • It's difficult to detect what is an ES Module and what is CommonJS without some form of user input, but they are trying.
  • *.mjs seems the most likely solution, unless they can accurately detect an ES Module without user-input

-- Original Answer --

This has been a hot potato for quite some time. Bottom line is that yes, Node will eventually support the ES2015 syntax for importing/exporting modules - most likely when the spec for loading modules is finalized and agreed upon.

Here is a good overview of what's holding NodeJS up. Essentially, they need to make sure that the new spec works for Node which is primarily conditional, synchronous loading and also HTML which is primarily asynchronous.

Nobody knows for sure right now, but I imagine Node will support import/export for static loading, in addition to the new System.import for dynamic loading - while still keeping require for legacy code.

Here's a few proposals on how Node might achieve this: