升级NextJS 15后ESLint已加插件仍提示未检测到,是否为误报?
问题
我有一个使用eslint.config.mjs的TypeScript项目,升级至NextJS 15后出现以下警告:
The Next.js plugin was not detected in your ESLint configuration. See https://nextjs.org/docs/app/building-your-application/configuring/eslint#migrating-existing-config
已按照相关建议修改eslint.config.mjs,加入Next.js插件及其推荐规则,配置内容如下:
import eslint from "@eslint/js"; import tseslint from "typescript-eslint"; import eslintPluginNext from "@next/eslint-plugin-next"; export default tseslint.config({ files: ["src/**/*.ts", "src/**/*.tsx"], ignores: ["src/__generated__/**/*"], plugins: { '@next/next': eslintPluginNext, // adding plugin }, extends: [ eslint.configs.recommended, ...tseslint.configs.strictTypeChecked, ...tseslint.configs.stylisticTypeChecked, ], languageOptions: { parserOptions: { projectService: true, tsconfigRootDir: import.meta.dirname, }, }, rules: { ...eslintPluginNext.configs.recommended.rules, // importing the rules "@typescript-eslint/no-unused-vars": [ "error", { args: "all", argsIgnorePattern: "^_", caughtErrors: "all", caughtErrorsIgnorePattern: "^_", destructuredArrayIgnorePattern: "^_", varsIgnorePattern: "^_", ignoreRestSiblings: true } ], "@typescript-eslint/restrict-template-expressions": [ "error", { allowNumber: true, allowBoolean: true, } ], } });
项目无其他ESLint配置文件,开发依赖如下:
{ "@biomejs/biome": "^1.9.4", "@graphql-codegen/cli": "^5.0.3", "@graphql-codegen/fragment-matcher": "^5.0.2", "@graphql-codegen/typescript-operations": "^4.3.1", "@graphql-codegen/typescript-react-apollo": "^4.3.2", "@swc/core": "^1.9.2", "@swc/helpers": "^0.5.15", "@types/node": "^22.9.0", "@types/react": "^18.3.12", "@types/react-dom": "^18.3.1", "dotenv": "^16.4.5", "eslint": "^9.15.0", "eslint-config-next": "^15.0.3", "graphql": "^16.9.0", "jest": "^29.7.0", "postcss": "^8.4.49", "shadcn": "^2.1.6", "tailwindcss": "^3.4.15", "ts-node": "^10.9.2", "tsconfig-paths": "^4.2.0", "typescript": "^5.6.3", "typescript-eslint": "^8.14.0" }
该警告在NextJS 14中未出现,请问是否属于误报?
分析与解决
这不是误报,问题出在当前ESLint配置写法不符合Next.js 15的检测逻辑。
Next.js 15的ESLint检测机制会检查配置中是否直接扩展了eslint-config-next提供的扁平配置,而手动引入插件和复制规则的方式,无法被其识别。
正确的配置方式应该直接扩展eslint-config-next的配置,无需手动处理插件和规则:
import eslint from "@eslint/js"; import tseslint from "typescript-eslint"; import nextEslintConfig from "eslint-config-next"; export default tseslint.config({ files: ["src/**/*.ts", "src/**/*.tsx"], ignores: ["src/__generated__/**/*"], extends: [ eslint.configs.recommended, ...tseslint.configs.strictTypeChecked, ...tseslint.configs.stylisticTypeChecked, ...nextEslintConfig, ], languageOptions: { parserOptions: { projectService: true, tsconfigRootDir: import.meta.dirname, }, }, rules: { "@typescript-eslint/no-unused-vars": [ "error", { args: "all", argsIgnorePattern: "^_", caughtErrors: "all", caughtErrorsIgnorePattern: "^_", destructuredArrayIgnorePattern: "^_", varsIgnorePattern: "^_", ignoreRestSiblings: true } ], "@typescript-eslint/restrict-template-expressions": [ "error", { allowNumber: true, allowBoolean: true, } ], } });
修改后,Next.js就能正确识别到ESLint配置中的Next.js插件和规则,警告会自动消失。
额外注意事项:
- 确保
eslint-config-next的版本与Next.js版本保持一致(当前均为15.0.3,无问题) - 无需手动导入
@next/eslint-plugin-next,eslint-config-next已包含该插件及所有必要规则配置
内容的提问来源于stack exchange,提问作者Viele
相关产品推荐
相关产品推荐

