You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何ESLint对空值合并运算符??报错?

ESLint无法识别空值合并运算符(??)的解决办法

问题详情

我的ESLint配置:

globals:
    require: true
    global: true
    $: true
env:
    browser: true
    es6: true
extends: 'eslint:recommended'
parserOptions:
    ecmaVersion: 2018
    sourceType: module

JavaScript代码语句:

let checkboxChecked = JSON.parse(localStorage.getItem('previouslyChecked')) ?? {};

ESLint报错:error Parsing error: Unexpected token ?,不想使用// eslint-disable-line,求解决思路。

解决步骤

  1. 问题根源:空值合并运算符??是ES2020(ES11)引入的新语法,而当前配置里的ecmaVersion设为2018,ESLint的解析器只能识别到ES2018及更早的语法,自然无法识别??。
  2. 核心修复:修改parserOptions中的ecmaVersion为2020,或者直接设置为latest(自动适配最新ECMAScript语法),修改后的配置如下:
globals:
    require: true
    global: true
    $: true
env:
    browser: true
    es6: true
extends: 'eslint:recommended'
parserOptions:
    ecmaVersion: 2020
    sourceType: module
  1. 额外排查:如果修改后仍报错,检查ESLint版本是否过旧——部分早期版本的ESLint对ES2020语法支持不完善,建议升级到最新稳定版。

内容的提问来源于stack exchange,提问作者michouël

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 22:01:36