【问题标题】:ES6 giving SyntaxError: Unexpected token ) [duplicate]ES6 给出 SyntaxError: Unexpected token ) [重复]
【发布时间】:2016-05-20 15:38:34
【问题描述】:

这是我的 Node.js 脚本:

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

在 Node.js v0.10.25(Ubuntu 2015.10 上的默认设置)上执行时,我收到有关括号的以下语法错误:

/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

我该如何解决?

【问题讨论】:

  • 据我所知,Node 0.10 中的 V8 版本不支持箭头函数。您可以使用 Babel 之类的工具将代码编译为 ES5、使用更新版本的 Node 或不使用不受支持的 ES6 功能来修复它。
  • @JamesAllardice - 箭头函数可能在节点上的--harmony 标志后面
  • @JamesAllardice:我正计划为此写一个答案(QA 风格),但你写得比我好,所以我把你的评论作为答案,谢谢!
  • @JaromandaX - 他们似乎没有。我刚刚在 0.10.40 中尝试过,this list from 0.10.2 也不包含它们。
  • @NicolasRaoul - 注意这里的问题是箭头函数的使用,而不是承诺。您没有引用全局 Promise 对象。如果你是(并且没有使用箭头函数)它仍然无法工作,但它会引发参考错误而不是语法错误(虽然不是我对你的答案的反对!)

标签: javascript node.js ecmascript-6 es6-promise arrow-functions


【解决方案1】:

Node 0.10 中的 V8 版本不支持箭头函数。

您可以通过以下任一方式修复它:

  • 使用 Babel 之类的工具将代码编译为 ES5
  • 或使用更新版本的 Node,例如 5.x
  • 使用箭头函数

(感谢 James Allardice)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-18
    • 2012-09-22
    • 1970-01-01
    • 1970-01-01
    • 2019-08-05
    • 2018-08-22
    相关资源
    最近更新 更多