14

I'm trying to use arrow functions in node v0.10.33 under Ubuntu 14.04 (I'm running node with --harmony flag), but I'm getting this error:

console.log( [1,2,3,4].map(x => x*x) );
                          ^
SyntaxError: Unexpected token >
    at Module._compile (module.js:439:25)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:906:3
hippietrail
  • 15,848
  • 18
  • 99
  • 158
Adrian Baran
  • 885
  • 7
  • 23
  • 1
    It's not supported on that node version, even with that flag. See http://stackoverflow.com/a/38241325/1385678 – Diego V Jul 18 '16 at 08:49

2 Answers2

9

This should work now in node v0.12.x, with the --harmony flag. Also note that you can get arrow functions in node using the babel package.

http://babeljs.io/docs/using-babel/

UPDATE:

As correctly indicated by Mike 'Pomax' Kamermans, the --harmony flag is not required when using Node.js >= 4.x. Please see https://nodejs.org/en/docs/es6/ for more information on the status of ES6 support in Node.js.

Christian Bankester
  • 2,044
  • 2
  • 15
  • 18
  • This should be the accepted answer. I can confirm that on v0.12.0 arrow functions are "supported." However, it appears that it is nothing more than a syntactical alternative to `function`, because it doesn't properly bind scope as the spec requires. Thus, they're basically useless until this is fixed. – Bailey Parker Jun 22 '15 at 07:45
  • @PhpMyCoder This works correctly in Node.js >= v4. `let foo = {a: 'a', foo(){ return () => this.a; }};` `foo.foo()(); // => 'a'` – Christian Bankester Nov 02 '15 at 16:51
  • Note that as of early 2016, both current versions of Node (4.* LTS and 5.* stable) support this without needing any flags. – Mike 'Pomax' Kamermans Apr 17 '16 at 17:43
5

Node, even with the harmony flag, doesn't support the fat arrow yet. Source.

Edit: Fun little fact, ES6 support is one of the reasons Node got forked into io.js. Check out their page on ES6 features - they provide a flag for the arrow functions, but discourage using it.

tydotg
  • 631
  • 4
  • 11
  • Thanks @tydotg, do you happen to know when this future will be available? – Adrian Baran Jan 25 '15 at 21:37
  • Not entirely sure what that reference page is trying to say, but I think the point is that it's not available in the v8 JavaScript engine yet, so it's not in node. It kind of looks like they are anticipating fat arrows coming to v8 in late March, so it would likely be available in the latest node release sometime after. – tydotg Jan 25 '15 at 21:45
  • Actually, I don't think 3.29 is a date, but a version number of v8 (which looks to have been released the end of last year). – tydotg Jan 25 '15 at 21:50
  • @AdrianBaran Many people have opted to start using transpilers like [`6to5`](https://6to5.org/) in order to start using ES6 features without needing full runtime support. – loganfsmyth Jan 25 '15 at 23:59
  • I think `6to5` will be the solution for me for now. Thanks @loganfsmyth. – Adrian Baran Jan 26 '15 at 01:10