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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37132031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:58:04  来源:igfitidea点击:

NodeJS plans to support import/export es6 (es2015) modules

javascriptnode.jsmoduleecmascript-6es6-modules

提问by Zorgatone

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? I haven't found an hint at all about this.

目前我对使用这两种方法中的任何一种都不太积极,NodeJS 维护者是否甚至计划支持 ES2015 模块?我还没有找到任何关于这个的提示。

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 是否会在不久的将来立即支持这些模块?

回答by CodingIntrigue

Node 13.2.0 & Above

节点 13.2.0 及以上

NodeJS 13.2.0now supports ES Modules without a flag However, the implementation is still marked as experimental so use in production with caution.

NodeJS 13.2.0现在支持没有标志的 ES 模块但是,该实现仍被标记为实验性的,因此在生产中谨慎使用。

To enable ESM support in 13.2.0, add the following to your package.json:

要在 13.2.0 中启用 ESM 支持,请将以下内容添加到您的package.json

{
  "type": "module"
}

All .js, .mjs(or files without an extension) will be treated as ESM.

所有.js, .mjs(或没有扩展名的文件)都将被视为 ESM。

There are a number of different options other than entire package.jsonopt-in, all of which are detailed in the Documentation for 13.2.0.

除了完全package.json选择加入之外,还有许多不同的选项,所有这些都在13.2.0文档中进行了详细说明

Node 13.1.0 & Below

节点 13.1.0 及以下

Those still using older versions of Node may want to try the esmmodule loader, which is a production-ready implementation of the ES Modules Spec for NodeJS:

那些仍在使用旧版本 Node 的人可能想尝试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-modulesflag, but there are major changes in the way modules can be loaded:

它仍然落后于--experimental-modules标志,但模块加载方式发生了重大变化:

  • package.typewhich can be either moduleor commonjs
    • type: "commonjs":
      • .jsis parsed as commonjs
      • default for entry point without an extension is commonjs
    • type: "module":
      • .jsis parsed as esm
      • does not support loading JSON or Native Module by default
      • default for entry point without an extension is esm
  • --type=[mode]to let you set the type on entry point. Will override package.typefor entry point.
  • A new file extension .cjs.
    • this is specifically to support importing commonjs in the modulemode.
    • 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.
  • --es-module-specifier-resolution=[type]
    • options are explicit(default) and 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=nodeto enable the commonjs specifier resolution algorithm
    • 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"
    • when enable all import 'thing.json'will go through the experimental loader independent of mode
    • based on whatwg/html#4315
  • You can use package.mainto set an entry point for a module
    • the file extensions used in main will be resolved based on the type of the module
  • package.type可以是modulecommonjs
    • type: "commonjs"
      • .js被解析为 commonjs
      • 没有扩展名的入口点的默认值是 commonjs
    • type: "module"
      • .js被解析为 esm
      • 默认不支持加载 JSON 或 Native Module
      • 没有扩展名的入口点的默认值是 esm
  • --type=[mode]让您在入口点设置类型。将覆盖package.type入口点。
  • 一个新的文件扩展名.cjs
    • 这是专门支持在module模式中导入commonjs的。
    • 这仅在 esm 加载器中,commonjs 加载器保持不变,但如果您使用完整的文件路径,扩展将在旧加载器中工作。
  • --es-module-specifier-resolution=[type]
    • 选项是explicit(默认)和node
    • 默认情况下,我们的加载器不允许在导入中使用可选的扩展名,模块的路径必须包含扩展名(如果有)
    • 默认情况下,我们的加载程序不允许导入具有索引文件的目录
    • 开发人员可以使用--es-module-specifier-resolution=node来启用 commonjs 说明符解析算法
    • 这不是“功能”,而是实验的实现。预计在标志被移除之前会发生变化
  • --experimental-json-loader
    • 导入 json 的唯一方法 "type": "module"
    • 当启用所有import 'thing.json'将通过独立于模式的实验加载器
    • 基于whatwg/html#4315
  • 您可以package.main用来设置模块的入口点
    • main 中使用的文件扩展名将根据模块的类型进行解析

17 January 2019

2019 年 1 月 17 日

Node 11.6.0still lists ES Modules as experimental, behind a flag.

Node 11.6.0仍然将 ES 模块列为实验性的,并带有标记。

13 September 2017

2017 年 9 月 13 日

NodeJS 8.5.0has 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:
https://github.com/nodejs/node/commit/c8a389e19f172edbada83f59944cad7cc802d9d5

NodeJS 主分支已更新,初步支持 ESM 模块:https:
//github.com/nodejs/node/commit/c8a389e19f172edbada83f59944cad7cc802d9d5

This should be available in the latest nightly (this can be installed via nvmto run alongside your existing install):
https://nodejs.org/download/nightly/

这应该在最新的每晚可用(这可以通过 nvm 安装以与现有安装一起运行):https: //nodejs.org/download/nightly/

And enabled behind the --experimental-modulesflag:

并在--experimental-modules标志后面启用:

package.json

包.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 badsolution is to use the .mjsfile extension. The takeaway from this is:

NodeJS 人员已经决定,最不坏的解决方案是使用.mjs文件扩展名。从中得出的结论是:

In other words, given two files foo.jsand bar.mjs, using import * from 'foo'will treat foo.jsas CommonJS while import * from 'bar'will treat bar.mjsas an ES6 Module

换句话说,给定两个文件foo.jsand bar.mjs, usingimport * from 'foo'foo.js视为 CommonJS 而import * from 'bar'bar.mjs视为 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.

目前,在 Node.js 甚至可以开始研究 ES6 模块的可支持实现之前,ES6 和虚拟机方面仍有许多规范和实现问题需要解决。工作正在进行中,但需要一些时间?-?我们目前至少在一年左右。

October 2016:

2016 年 10 月:

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:

Node.JS 的一位开发人员最近参加了 TC-39 会议,并写了一篇关于 Node.JS 实现的阻塞器的精彩文章:

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

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.
  • *.mjsseems the most likely solution, unless they can accurately detect an ES Module without user-input
  • ES Modules 静态分析,CommonJS 评估
  • CommonJS 模块允许猴子修补导出,ES 模块目前不支持
  • 如果没有某种形式的用户输入,很难检测什么是 ES 模块,什么是 CommonJS,但他们正在尝试。
  • *.mjs似乎是最可能的解决方案,除非他们可以在没有用户输入的情况下准确检测 ES 模块

-- 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 modulesis finalized and agreed upon.

一段时间以来,这一直是一个烫手山芋。最重要的是,Node 最终将支持用于导入/导出模块的 ES2015 语法——最有可能是在最终确定并商定加载模块规范时。

Here is a good overviewof 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.

这里很好地概述了阻碍 NodeJS 的因素。本质上,他们需要确保新规范适用于主要是条件同步加载的 Node 以及主要是异步的 HTML。

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

现在没有人确切知道,但我想 Node 将支持import/export静态加载,除了System.import动态加载的新功能 - 同时仍然保留require遗留代码。

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

下面是一些关于 Node 如何实现这一目标的建议: