如何在 nodejs 中使用 ES7?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44027764/
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-09-02 18:13:47  来源:igfitidea点击:

How can I use ES7 in nodejs?

node.jsecmascript-7

提问by Majid Parvin

How can I use ES7 (ECMAScript 2016)in nodejs? How can I use it in production?

如何在nodejs 中使用ES7 (ECMAScript 2016)?我如何在生产中使用它?

And in which version of node, I don't need using any module for doing that?

在哪个版本的节点中,我不需要使用任何模块来做到这一点?

Any help is appreciated.

任何帮助表示赞赏。

回答by rsp

Note: This question was explicitly about ES2016 (ES7). See updates below for ES2017 (ES8).

注意:这个问题明确是关于 ES2016 (ES7)。请参阅下面的 ES2017 (ES8) 更新。

The ES7 had only two main features:

ES7 只有两个主要特性:

  1. Array.prototype.includes
  2. **(the exponentiation operator)
  1. Array.prototype.includes
  2. **(求幂运算符)

See on Mozilla Development Network for more info:

有关更多信息,请参阅 Mozilla 开发网络:

According to node.green those are available in Node since, respectively:

根据 node.green,它们分别在 Node 中可用:

  • 5.0 with harmony flag and 6.0 with no flag (Array.prototype.includes)
  • 6.5 with harmony flag and 7.0 with no flag (exponentiation)
  • 5.0 带和谐标志,6.0 不带标志(Array.prototype.includes)
  • 6.5 有和声标志,7.0 没有标志(取幂)

See:

看:

The exponentiation is the last ES7 feature that was added to Node so if you want to use all ES7 features with no flags then you need at least Node 7.0. If you can use the --harmonyflag then you can use at least Node 6.5.

求幂是添加到 Node 的最后一个 ES7 特性,所以如果你想使用所有没有标志的 ES7 特性,那么你至少需要 Node 7.0。如果您可以使用该--harmony标志,那么您至少可以使用 Node 6.5。

The 6.x version is LTS (Long Term Support) so you may want to prefer it over 7.x but you'll need the flag to use the ES7 features.

6.x 版本是 LTS(长期支持),因此您可能希望它比 7.x 更受欢迎,但您需要该标志才能使用 ES7 功能。

At the time of this writing the current versions of Node are:

在撰写本文时,Node 的当前版本是:

  • v6.10.3 LTS (Recommended For Most Users)
  • v7.10.0 Current (Latest Features)
  • v6.10.3 LTS(推荐给大部分用户)
  • v7.10.0 当前(最新功能)

The v8.0 LTS will be released shortly - currently you can use the nightly builds of Node 8.0.

v8.0 LTS 将很快发布 - 目前您可以使用 Node 8.0 的夜间版本。

For more info on the release schedule - see:

有关发布时间表的更多信息 - 请参阅:

For other versions to download - see:

要下载其他版本 - 请参阅:

Update for ES2017

ES2017 更新

Answering the question from the comments, async/await is a feature of ES2017 (ES8), not ES2016 (ES7) as this question was about. See:

从评论中回答问题,async/await 是 ES2017 (ES8) 的一个特性,而不是这个问题所涉及的 ES2016 (ES7)。看:

  • Specification: ECMAScript Latest Draft (ECMA-262) The definition of 'async function' in that specification.
  • Status: Draft
  • Comment: Initial definition in ES2017.
  • 规范:ECMAScript 最新草案 (ECMA-262) 该规范中“异步函数”的定义。
  • 状态:草稿
  • 评论:ES2017 中的初始定义。

async/await in Node

Node 中的异步/等待

You can use async/await in:

您可以在以下情况下使用 async/await:

  • Node 7.0 with the --harmonyflag
  • Node 7.6 without any flag
  • 带有--harmony标志的节点 7.0
  • 没有任何标志的节点 7.6

For more info see:

有关更多信息,请参阅:

In places where you don't have native support for asyncand awaityou can use Babel:

在你没有原生支持的地方asyncawait你可以使用 Babel:

or with a slightly different syntax a generator based approach like in coor Bluebird coroutines:

或者使用稍微不同的语法基于生成器的方法,例如 inco或 Bluebird 协程:

See those answers for more info:

有关更多信息,请参阅这些答案:

They include more info about the compatibility and possible workarounds.

它们包含有关兼容性和可能的​​解决方法的更多信息。

回答by Adrian Makowski

There is this site http://node.green/that shows the level of compatibility of different versions of NodeJS with different versions of ES standards.

有这个站点http://node.green/显示了不同版本的 NodeJS 与不同版本的 ES 标准的兼容性级别。

回答by Sergaros

You need to use --harmony flag for example:

例如,您需要使用 --harmony 标志:

node --harmony server.js