50

Is Node.js supporting => function keyword alias already? If yes, starting from which version? How to enable this language extension?

(function() { console.log('it works!') })()

Becomes

(() => { console.log('it works!') })()
Diego V
  • 6,189
  • 7
  • 40
  • 45
exebook
  • 32,014
  • 33
  • 141
  • 226
  • 2
    Language features like arrow functions aren't tied to specific Node.js versions, necessarily. They're dependent on V8 itself and can be enabled currently with [`--harmony` options](http://stackoverflow.com/q/13351965/15031). Run `node --v8-options` to see the full list. – Jonathan Lonowski Oct 28 '13 at 20:30
  • 1
    Up to date answer: https://stackoverflow.com/a/38241325/1385678 – Diego V Oct 22 '16 at 13:30
  • 3
    `=>` isn't merely an *alias* for `function`. Arrow functions bind to `this` in the lexical context of their declaration (except in node 4) in addition to returning their body in the syntactic variant without braces. – Touffy Mar 03 '17 at 17:02

4 Answers4

77

In short: yes, arrow functions are reasonably well supported in Node.js since version 4.4.5.

Completely correct support starts with version 6. Initial support was introduced as far as v0.12 but is was very incomplete and disabled by default until v4.0 when it got better. See Node's ES6 compatibility table for details: http://node.green/#ES2015-functions-arrow-functions.

Diego V
  • 6,189
  • 7
  • 40
  • 45
  • node version 4.2.4 already supports arrow functions: `((a, b) => a + 3 * b)(2, 4)` gives you 14. downvote, since this answer makes you believe it is only supported since version 4.4.5. Since it is supported since (at least) 4.2.4 this means that especially the 4.3 LTS release *does* support arrow functions. Also the linked site does not mention 4.4.5 as being special in any way. Note though that arrow functions apparently have incorrect precedence according to the linked site, the first node version to support this properly seems to be 6.4.0. Thus I believe: Support since 4.0, Proper since 6.4 – scravy Mar 02 '17 at 18:15
  • @scravy, your single test on version 4.2.4 proves nothing else than some support of that syntax was there. The linked site has obviously changed since I first checked it and no longer show tests for those old versions but [back then](https://web.archive.org/web/20160320092718/http://node.green/#arrow-functions) you could see that v4.4.0 will still fail in various cases. I edited my answer to include that proper support starts on version 6. – Diego V Mar 03 '17 at 17:55
  • After thinking more about your report of this working on 4.2.4, I agree it's important to mention that this feature was enabled by default on [v4.0](https://nodejs.org/en/blog/release/v4.0.0/) for the very first time, so I also added that to the answer. Thanks. – Diego V Mar 03 '17 at 18:38
32

The syntax you're referring to is "arrow function" syntax. It is a feature of ECMAScript 6, also known as "Harmony". The ES6 standard is now finalized, but engines are still implementing its new features.

The V8 now has arrow function support. Node runs on the V8 engine, but it can take some time for Node to incorporate the latest version into its code base.

Whenever it is added, it might possibly be enabled only via a --harmony command-line flag.

Community
  • 1
  • 1
apsillers
  • 112,806
  • 17
  • 235
  • 239
20

You can follow this issue: https://code.google.com/p/v8/issues/detail?id=2700

Currently (as 02.05.2014) arrow functions have been implemented and waiting until this functionality will be landed in v8: https://codereview.chromium.org/160073006/

After then we'll need to wait, until v8 version with arrow function would be integrated into Node.JS. You can follow Node.JS changelog there: https://github.com/joyent/node/blob/master/ChangeLog (search for "v8: upgrade to ....")

Valentyn Shybanov
  • 19,331
  • 7
  • 66
  • 59
4

kangax's compatibility tables can keep you up-to-date with what is currently available in Node.

Experimental features can be enabled using the instructions on this page:

All shipping features are turned on by default on Node.js

Staged feature require a runtime flag: --es_staging (or its synonym, --harmony)

In progress features can be activated individually by their respective harmony flag (e.g. --harmony_destructuring) but this is highly discouraged

joeytwiddle
  • 29,306
  • 13
  • 121
  • 110