使用
eslint.config.mjs
的 Typescript 项目。升级到 NextJS 后,开始看到此警告消息:
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
以包含规则和插件。按照这篇文章的建议:NextJS 15 下的正确 eslint 配置
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,
}
],
}
});
没有其他 lint 配置文件,例如.eslintrc.json 等
开发部门:
"@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"
这是误报吗?之前的 Next 14 中没有此警告
将下一个 eslint 内容放入单独的对象中会有所帮助:
export default tseslint.config(
{...},
{
plugins: {
'@next/next': eslintPluginNext
},
{
rules: ...eslintPluginNext.configs.recommended.rules
}
}
)
linter 以一种有点晦涩但有意的方式不同地对待单独的对象(或者某些插件本身的行为有所不同,具体取决于它们是否“单独”)。 如果仅此方法无法解决问题,您还可以尝试将此逻辑移至列表底部/使其成为可变参数的最后一个,因此它将优先于具有相同名称的其他值。