【问题标题】:SyntaxError: Unexpected token '?'SyntaxError:意外的令牌'?'
【发布时间】:2021-03-31 10:59:28
【问题描述】:

错误

SyntaxError: Unexpected token '?'

这里是代码

const result1 = (null || undefined) ?? 'OK'; 
console.log(result1); // 'OK'

当我在Node.js 中运行这个程序时,它会引发 unexpected token error

【问题讨论】:

  • 究竟是什么错误?
  • 您使用的版本太旧。如果我没记错的话,它是在节点 14 中添加的。
  • 这是在 Node.js 14.0.0 中添加的。你运行的是什么版本?
  • 当您遇到涉及新语言功能的错误时,您应该始终首先查看的是 MDN 中的兼容性图表。 developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
  • 代码片段在阅读问题的人的浏览器中执行。试图通过在读者环境中运行代码来展示特定于您的特定环境的故障没有多大意义。

标签: javascript node.js web


【解决方案1】:

nullish coalescing operator 是 2020 年 6 月在ECMAScript 2020 中新增的(截至原始问题日期大约六个月前)。

如果您在代码中使用 ECMAScript 2020 功能,请确保使用完全实现 ECMAScript 2020 (or at least the features you are using) 的 ECMAScript 实现。

【讨论】:

    【解决方案2】:

    假设您使用的是 Windows,download the LTS or current

    在此之后重新启动您的机器并确保在您的包 json 中您没有指定小于 13 的 engine(可选)

    { "engines" : { "node" : ">14" } }
    

    运行你的脚本

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-21
      • 2017-08-05
      • 2019-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多