0

Here is my Node.js script:

pDownload("http://serv/file", "localfile")
  .then( ()=> console.log('downloaded file no issues...'))
  .catch( e => console.error('error while downloading', e));

function pDownload(url, dest){} // Yes empty, for now

When executing on Node.js v0.10.25 (default on Ubuntu 2015.10) I receive this syntax error about the parenthesis:

/home/nico/main.js:2
  .then( ()=> console.log('downloaded file no issues...'))
          ^
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:902:3

How can I fix it?

Nicolas Raoul
  • 58,567
  • 58
  • 222
  • 373
  • 3
    The version of V8 in Node 0.10 doesn't support arrow functions as far as I know. You can fix it by using something like Babel to compile your code to ES5, using a more recent version of Node or not using unsupported ES6 features. – James Allardice Feb 10 '16 at 10:30
  • @JamesAllardice - arrow functions MAY be behind `--harmony` flag on node – Jaromanda X Feb 10 '16 at 10:32
  • @JamesAllardice: I was planning to write an answer for that (QA style), but you wrote it better than me so I made your comment an answer, thanks! – Nicolas Raoul Feb 10 '16 at 10:33
  • @JaromandaX - They don't appear to be. I just tried it in 0.10.40 and [this list from 0.10.2](http://www.2ality.com/2013/04/nodejs-harmony.html) doesn't include them either. – James Allardice Feb 10 '16 at 10:34
  • @NicolasRaoul - Note that the problem here is the use of the arrow function, not promises. You are not referencing the global `Promise` object. If you were (and didn't use the arrow functions) it would still not work but it would throw a reference error instead of a syntax error (not my downvote on your answer though!) – James Allardice Feb 10 '16 at 10:36
  • yeah, ... always thought Ubuntu wanted to be closer to `bleeding edge` than `20 versions behind the times` ! – Jaromanda X Feb 10 '16 at 10:36

1 Answers1

-1

The version of V8 in Node 0.10 doesn't support arrow functions.

You can fix it by either:

  • Using something like Babel to compile your code to ES5
  • Or using a more recent version of Node, like 5.x
  • Doing with arrow functions

(Thanks James Allardice)

Nicolas Raoul
  • 58,567
  • 58
  • 222
  • 373