26

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

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

Any help is appreciated.

Majid Parvin
  • 4,499
  • 5
  • 29
  • 47

3 Answers3

28

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

The ES7 had only two main features:

  1. Array.prototype.includes
  2. ** (the exponentiation operator)

See on Mozilla Development Network for more info:

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

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

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 --harmony flag then you can use at least 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.

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

  • v6.10.3 LTS (Recommended For Most Users)
  • v7.10.0 Current (Latest Features)

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

For more info on the release schedule - see:

For other versions to download - see:

Update for ES2017

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

  • Specification: ECMAScript Latest Draft (ECMA-262) The definition of 'async function' in that specification.
  • Status: Draft
  • Comment: Initial definition in ES2017.

async/await in Node

You can use async/await in:

  • Node 7.0 with the --harmony flag
  • Node 7.6 without any flag

For more info see:

In places where you don't have native support for async and await you can use Babel:

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

See those answers for more info:

They include more info about the compatibility and possible workarounds.

Community
  • 1
  • 1
rsp
  • 107,747
  • 29
  • 201
  • 177
  • 1
    whats about **async/await** feature? – Majid Parvin May 17 '17 at 16:40
  • @MajidParvin: That's still experimental (not part of ES7) is going to be released this year with ES2017. – Felix Kling May 17 '17 at 18:27
  • @MajidParvin async/await is a feature of ES2017 (ES8) - not ES2016 (ES7) as this question was about. See the updated answer - I added info on ES2017 and async/await. – rsp May 18 '17 at 11:29
14

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

4

You need to use --harmony flag for example:

node --harmony server.js
Sergaros
  • 821
  • 1
  • 5
  • 14