NextJS项目升级后ESLint无法加载prettier插件问题求助
解决ESLint无法加载eslint-plugin-prettier模块的问题
问题根源
升级依赖后出现以下潜在冲突:
- 依赖包未正确安装或缓存残留导致模块找不到
- 旧版解析器
babel-eslint已废弃,与ESLint 8.x版本不兼容 - ESLint配置中存在重复插件声明,引发加载逻辑冲突
解决方案步骤
1. 清理并重新安装依赖
- 删除项目根目录下的
node_modules文件夹、package-lock.json(或yarn.lock)文件 - 执行命令重新拉取依赖:
npm install # 或使用yarn yarn install - 可选:清理npm缓存避免缓存异常:
npm cache clean --force
2. 修复ESLint配置问题
替换废弃的解析器
babel-eslint已停止维护,无法适配ESLint 8.x,需替换为@babel/eslint-parser:
- 安装新解析器:
npm install @babel/eslint-parser --save-dev - 修改
.eslintrc中的parser字段:parser: '@babel/eslint-parser',
简化插件声明
plugin:prettier/recommended扩展会自动引入prettier插件并配置规则,因此可以移除plugins数组里的prettier项,避免重复声明:
plugins: ['react-hooks'] // 移除'prettier'
确保扩展顺序正确
plugin:prettier/recommended必须放在extends数组最后,确保它能覆盖之前扩展中的冲突规则:
extends: [ 'airbnb', 'airbnb/hooks', 'eslint:recommended', 'plugin:prettier/recommended' // 保持在最后 ],
3. 验证修复效果
执行ESLint检查命令确认问题解决:
npx eslint .
修改后的完整.eslintrc示例
module.exports = { extends: [ 'airbnb', 'airbnb/hooks', 'eslint:recommended', 'plugin:prettier/recommended' ], parser: '@babel/eslint-parser', parserOptions: { ecmaVersion: 9, ecmaFeatures: { experimentalObjectRestSpread: true, impliedStrict: true, classes: true } }, env: { browser: true, node: true, jest: true }, settings: { 'import/internal-regex': '^~' }, rules: { strict: 0, 'prettier/prettier': 'error', 'no-console': 'error', 'no-debugger': 'error', 'no-alert': 'error', 'no-await-in-loop': 0, 'no-return-assign': ['error', 'except-parens'], 'no-restricted-syntax': [ 2, 'ForInStatement', 'LabeledStatement', 'WithStatement' ], 'no-unused-vars': [ 'error', { args: 'all', ignoreRestSiblings: true, argsIgnorePattern: '^_', caughtErrors: 'all' } ], 'prefer-const': [ 'error', { destructuring: 'all' } ], 'arrow-body-style': [2, 'as-needed'], 'no-unused-expressions': [ 2, { allowTaggedTemplates: true } ], 'no-param-reassign': [ 2, { props: false } ], 'import/order': [ 'error', { groups: [ 'builtin', 'external', 'internal', 'unknown', 'index', ['sibling', 'parent'], 'object' ], 'newlines-between': 'always', pathGroups: [ { pattern: '~/**', group: 'internal' } ], pathGroupsExcludedImportTypes: ['builtin', 'external'] } ], 'import/prefer-default-export': 0, import: 0, 'func-names': 0, 'space-before-function-paren': 0, 'comma-dangle': 0, 'max-len': 0, 'import/extensions': 0, 'import/no-named-as-default': 0, 'no-underscore-dangle': 0, 'consistent-return': 0, 'react/display-name': 1, 'react-hooks/rules-of-hooks': 'error', 'react-hooks/exhaustive-deps': 'warn', 'react/no-array-index-key': 0, 'react/react-in-jsx-scope': 0, 'react/prefer-stateless-function': 0, 'react/forbid-prop-types': 0, 'react/no-unescaped-entities': 0, 'react/static-property-placement': [1, 'static public field'], 'jsx-a11y/accessible-emoji': 0, 'jsx-a11y/click-events-have-key-events': 0, 'jsx-a11y/no-static-element-interactions': 0, 'jsx-a11y/no-noninteractive-element-interactions': 0, 'react/jsx-props-no-spreading': [ 2, { html: 'enforce', custom: 'ignore', explicitSpread: 'ignore' } ], 'react/require-default-props': 0, 'react/jsx-filename-extension': [ 1, { extensions: ['.js', '.jsx'] } ], radix: 0, 'no-shadow': [ 2, { hoist: 'all', allow: ['resolve', 'reject', 'done', 'next', 'err', 'error'] } ], quotes: [ 2, 'single', { avoidEscape: true, allowTemplateLiterals: true } ], 'jsx-a11y/href-no-hash': 'off', 'jsx-a11y/anchor-is-valid': [ 'warn', { aspects: ['invalidHref'] } ] }, plugins: ['react-hooks'] };
内容的提问来源于stack exchange,提问作者Abhijeet Khengar
相关产品推荐
相关产品推荐

