Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use ES7 in nodejs?

Tags:

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.

like image 685
Majid Parvin Avatar asked May 17 '17 14:05

Majid Parvin


People also ask

How do I use ES7 in node JS?

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.

What is the difference between ES6 and ES7?

Introducing the new features that ECMAScript 2016 (ES7) adds to JavaScript. Since ECMAScript 2015 (also known as ES6) was released, it has introduced a huge set of new features. They include arrow functions, sets, maps, classes and destructuring, and much more.

What is js ES7?

ES7 introduces a new mathematical operator called exponentiation operator. This operator is similar to using Math.pow() method. Exponentiation operator is represented by a double asterisk **. The operator can be used only with numeric values. The syntax for using the exponentiation operator is given below −


1 Answers

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:

  • Web/JavaScript/Reference/Global_Objects/Array/includes
  • Web/JavaScript/Reference/Operators/Arithmetic_Operators#Exponentiation_(**)

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:

  • http://node.green/#ES2016-features-Array-prototype-includes
  • http://node.green/#ES2016-features-exponentiation------operator

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:

  • https://github.com/nodejs/LTS

For other versions to download - see:

  • https://nodejs.org/en/download/

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:

  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function
  • 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:

  • http://node.green/#ES2017-features-async-functions

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

  • https://babeljs.io/docs/plugins/transform-async-to-generator/

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

  • https://www.npmjs.com/package/co
  • http://bluebirdjs.com/docs/api/promise.coroutine.html

See those answers for more info:

  • try/catch blocks with async/await
  • node.js ~ constructing chained sequence of Promise resolves
  • How to run Generator Functions in Parallel?
  • node.js ~ constructing chained sequence of Promise resolves
  • Using async/await + Bluebird to promisifyAll
  • jQuery: Return data after ajax call success

They include more info about the compatibility and possible workarounds.

like image 70
rsp Avatar answered Sep 20 '22 13:09

rsp