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

React项目npm run build触发JSX缩进ESLint错误求助

React生产构建触发ESLint缩进错误,常规方案无效

问题现象

使用npm start启动开发服务时应用正常,但执行npm run build构建生产版本时,触发一系列react/jsx-indent及react/jsx-closing-tag-location缩进错误。

具体错误信息

[eslint] 
src/App.js
  Line 9:2:    Expected indentation of 2 space characters but found 1    react/jsx-indent
  Line 10:2:   Expected indentation of 3 space characters but found 1    react/jsx-indent
  Line 11:9:   Expected indentation of 3 space characters but found 8    react/jsx-indent
  Line 12:9:   Expected indentation of 10 space characters but found 8   react/jsx-indent
  Line 14:9:   Expected indentation of 3 space characters but found 8    react/jsx-indent
  Line 15:9:   Expected indentation of 10 space characters but found 8   react/jsx-indent
  Line 25:9:   Expected indentation of 10 space characters but found 8   react/jsx-indent
  Line 27:11:  Expected indentation of 12 space characters but found 10  react/jsx-indent
  Line 29:25:  Expected indentation of 12 space characters but found 10  react/jsx-indent
  Line 31:9:   Expected indentation of 10 space characters but found 8   react/jsx-indent
  Line 31:9:   Expected closing tag to match indentation of opening      react/jsx-closing-tag-location

当前ESLint配置(.eslintrc.js)

module.exports = {
  env: {
    browser: true,
    es6: true,
  },
  extends: [
    'plugin:react/recommended',
    'airbnb',
  ],
  globals: {
    Atomics: 'readonly',
    SharedArrayBuffer: 'readonly',
  },
  parserOptions: {
    ecmaFeatures: {
      jsx: true,
    },
    ecmaVersion: 2018,
    sourceType: 'module',
  },
  parser: 'babel-eslint',
  plugins: [
    'react',
  ],
  rules: {
    indent: 'off',
    "react/jsx-indent": [2, 2] ,
    'template-curly-spacing': 'off',
    'import/extensions': 0,
    'react/prop-types': 0,
    'linebreak-style': 0,
    'react/state-in-constructor': 0,
    'import/prefer-default-export': 0,
    'max-len': [
      2,
      250,
    ],
    'no-multiple-empty-lines': [
      'error',
      {
        max: 1,
        maxEOF: 1,
      },
    ],
    'no-underscore-dangle': [
      'error',
      {
        allow: [
          '_d',
          '_dh',
          '_h',
          '_id',
          '_m',
          '_n',
          '_t',
          '_text',
        ],
      },
    ],
    'object-curly-newline': 0,
    'react/jsx-filename-extension': 0,
    'react/jsx-one-expression-per-line': 0,
    'jsx-a11y/click-events-have-key-events': 0,
    'jsx-a11y/alt-text': 0,
    'jsx-a11y/no-autofocus': 0,
    'jsx-a11y/no-static-element-interactions': 0,
    'react/no-array-index-key': 0,
    'jsx-a11y/anchor-is-valid': [
      'error',
      {
        components: [
          'Link',
        ],
        specialLink: [
          'to',
          'hrefLeft',
          'hrefRight',
        ],
        aspects: [
          'noHref',
          'invalidHref',
          'preferButton',
        ],
        "import/no-extraneous-dependencies": ["error", {"devDependencies": true}]
      },
    ],
  },
};

已尝试的无效方案

  • 显式配置react/jsx-indent规则为2空格
  • 执行npm run lint -- --fix自动修复
  • 尝试禁用lint检查
  • 修改VSCode的缩进设置

有效解决方案建议

1. 修复ESLint配置中的嵌套错误

你的jsx-a11y/anchor-is-valid规则配置里错误嵌套了import/no-extraneous-dependencies规则,这会导致ESLint配置解析异常,规则无法正确生效。把这条规则移到外层rules对象中:

// 修改后的rules部分
rules: {
  // ...其他规则
  'jsx-a11y/anchor-is-valid': [
    'error',
    {
      components: [
        'Link',
      ],
      specialLink: [
        'to',
        'hrefLeft',
        'hrefRight',
      ],
      aspects: [
        'noHref',
        'invalidHref',
        'preferButton',
      ],
    },
  ],
  "import/no-extraneous-dependencies": ["error", {"devDependencies": true}]
}

2. 补充配置JSX缩进相关规则

添加react/jsx-indent-props规则,确保JSX属性缩进与标签缩进一致,同时明确react/jsx-closing-tag-location规则:

rules: {
  // ...其他规则
  "react/jsx-indent": [2, 2],
  "react/jsx-indent-props": [2, 2],
  "react/jsx-closing-tag-location": "error"
}

3. 确保ESLint与React插件版本兼容

版本不匹配可能导致规则失效,执行以下命令更新到兼容版本:

npm update eslint eslint-plugin-react --save-dev

4. 清除缓存并重新执行修复

配置修正后,先清除ESLint缓存再执行修复:

npx eslint --cache --clear
npm run lint -- --fix

5. 检查构建脚本的lint触发逻辑

查看package.json中的build脚本,确认是否强制先执行lint检查,例如:

"scripts": {
  "build": "npm run lint && react-scripts build"
}

确保修复后的代码能通过lint检查再执行构建。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 14:40:29