Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
549 views
in Technique[技术] by (71.8m points)

javascript - NodeJS计划支持导入/导出es6(es2015)模块(NodeJS plans to support import/export es6 (es2015) modules)

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.

(当前,NodeJS仅使用CommonJS语法来加载模块,如果您确实要使用标准ES2015模块语法,则必须事先对其进行转换,或者在运行时使用外部模块加载器。)

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?

(目前,我不太愿意使用这两种方法,NodeJS维护人员是否计划支持ES2015模块?)

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

(目前,NodeJS 6.x声称支持96%的ES2015功能,但没有任何模块参考( NodeJS ES2105支持链接 )。)

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

(您是否知道在不久的将来NodeJS是否将立即支持这些模块?)

  ask by Zorgatone translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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可以是modulecommonjs)

    • 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 。)

    • this is specifically to support importing commonjs in the module mode.

      (这专门支持在module模式下导入commonjs。)

    • this is only in the esm loader, the commonjs loader remains untouched, but the extension will work in the old loader if you use the full file path.

      (这仅在esm加载程序中,commonjs加载程序保持不变,但是如果您使用完整的文件路径,则扩展名将在旧的加载程序中工作。)

  • --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
    • the only way to import json when "type": "module"

      ("type": "module"时导入json的唯一方法)

    • when enable all import 'thing.json' will go through the experimental loader independent of mode

      (当启用所有import 'thing.json'将通过实验加载程序,与模式无关)

    • based on whatwg/html#4315

      (基于whatwg / html#4315)

  • You can use package.main to set an entry point for a module

    (您可以使用package.main设置模块的入口点)

    • the file extensions used in main will be resolved based on the type of the module

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...