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

添加@babel/eslint-parser后ESLint规则失效求助

ESLint配置失效问题:添加@babel/eslint-parser后规则不生效的解决方案

你的核心问题是**.eslintrc.json存在JSON语法错误**,parserOptions.babelOptions对象未闭合,导致ESLint无法正确解析整个配置文件,所有规则自然不会被读取执行。

错误位置说明

在你的配置中,parserOptions内的babelOptions对象只开启了{但未添加对应的闭合},破坏了JSON结构:

"parserOptions": {
    "requireConfigFile": false,
    "ecmaVersion": "latest",
    "sourceType": "module",
    "babelOptions": {
            "plugins": [
              "@babel/plugin-syntax-import-assertions"
            ]
// 此处缺失闭合的 }
},

修复后的完整配置

{
    "env": {
        "browser": true,
        "es2021": true,
        "mocha": true
    },
    "extends": [
        "eslint:recommended",
        "plugin:import/errors",
        "plugin:import/warnings"
    ],
    "parser": "@babel/eslint-parser",
    "parserOptions": {
        "requireConfigFile": false,
        "ecmaVersion": "latest",
        "sourceType": "module",
        "babelOptions": {
            "plugins": [
                "@babel/plugin-syntax-import-assertions"
            ]
        } // 补上这个闭合括号
    },
    "rules": {
        "@babel/indent": ["error", 4],
        "linebreak-style": ["error", "windows"],
        "quotes": ["error", "single"],
        "semi": ["error", "always"],
        "prefer-template": ["error"],
        "block-spacing": ["error", "always"],
        "spaced-comment": ["error", "always", {
            "line": {
                "markers": ["/"],
                "exceptions": ["-", "+"]
            },
            "block": {
                "markers": ["!"],
                "exceptions": ["*", "+", "-"],
                "balanced": true
            }
        }],
        "space-in-parens": ["error", "always", {
            "exceptions": ["()", "empty", "{}", "[]"]
        }],
        "object-curly-spacing": ["error", "always", { "arraysInObjects": false }],
        "no-trailing-spaces": ["error", { "ignoreComments": true }]
    }
}

额外注意点

  • @babel/indent规则属于@babel/eslint-plugin,需要先安装该插件才能让规则生效:
    npm install @babel/eslint-plugin --save-dev
    
  • 确保@babel/plugin-syntax-import-assertions已安装,否则导入JSON的断言语法无法被正确解析:
    npm install @babel/plugin-syntax-import-assertions --save-dev
    

内容的提问来源于stack exchange,提问作者david borg

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 07:25:31