ESLint 插件
了解如何使用和配置 ESLint 插件,以捕获 Next.js 应用中的常见问题和隐患。
Next.js 提供了一个 ESLint 配置包 eslint-config-next,让你可以轻松捕获应用中的常见问题。它包含 @next/eslint-plugin-next 插件,以及来自 eslint-plugin-react 和 eslint-plugin-react-hooks 的推荐规则集。
该包提供两种主要配置:
eslint-config-next:包含 Next.js、React 和 React Hooks 规则的基础配置。同时支持 JavaScript 和 TypeScript 文件。eslint-config-next/core-web-vitals:包含基础配置中的所有内容,并将影响 Core Web Vitals 的规则从警告升级为错误。推荐大多数项目使用。
此外,对于 TypeScript 项目:
eslint-config-next/typescript:添加来自typescript-eslint的 TypeScript 专用 linting 规则。请与 base 或 core-web-vitals 配置配合使用。
设置 ESLint
使用 ESLint CLI(flat config)快速启用 linting:
安装 ESLint 和 Next.js 配置:
pnpm
pnpm add -D eslint eslint-config-nextnpm
npm i -D eslint eslint-config-nextyarn
yarn add --dev eslint eslint-config-nextbun
bun add -d eslint eslint-config-next使用 Next.js 配置创建
eslint.config.mjs:
import { defineConfig, globalIgnores } from 'eslint/config'
import nextVitals from 'eslint-config-next/core-web-vitals'
const eslintConfig = defineConfig([
...nextVitals,
// Override default ignores of eslint-config-next.
globalIgnores([
// Default ignores of eslint-config-next:
'.next/**',
'out/**',
'build/**',
'next-env.d.ts',
]),
])
export default eslintConfig
运行 ESLint:
pnpm
pnpm exec eslint .npm
npx eslint .yarn
yarn eslint .bun
bunx eslint .
须知:
eslint-config-next支持 ESLint 9 和 ESLint 10。ESLint 10 要求 Node.js^20.19.0、^22.13.0或>=24。- 从
eslint-config-next@16.4.0开始,默认的 React 版本检测可与 ESLint 10 配合工作。你可以移除为规避context.getFilename错误而添加的settings.react.version覆盖配置。eslint-config-next中包含的部分插件尚未在其 peer 依赖中列出 ESLint 10,因此你的包管理器可能会显示 peer 依赖警告,或者在启用严格 peer 依赖时失败。
参考
eslint-config-next 包包含来自以下 ESLint 插件的 recommended 规则集:
规则
其中包含的 @next/eslint-plugin-next 规则有:
| 在推荐配置中启用 | 规则 | 描述 |
|---|---|---|
| ✓ | @next/next/google-font-display | 强制使用 Google Fonts 时采用 font-display 行为。 |
| ✓ | @next/next/google-font-preconnect | 确保在使用 Google Fonts 时同时使用 preconnect。 |
| ✓ | @next/next/inline-script-id | 强制在带有内联内容的 next/script 组件上使用 id 属性。 |
| ✓ | @next/next/next-script-for-ga | 为 Google Analytics 使用内联脚本时,优先使用 next/script 组件。 |
| ✓ | @next/next/no-assign-module-variable | 防止对 module 变量进行赋值。 |
| ✓ | @next/next/no-async-client-component | 防止客户端组件成为 async 函数。 |
| ✓ | @next/next/no-before-interactive-script-outside-document | 防止在 pages/_document.js 之外使用 next/script 的 beforeInteractive 策略。 |
| ✓ | @next/next/no-css-tags | 防止手动添加样式表标签。 |
| ✓ | @next/next/no-document-import-in-page | 防止在 pages/_document.js 之外导入 next/document。 |
| ✓ | @next/next/no-duplicate-head | 防止在 pages/_document.js 中重复使用 <Head>。 |
| ✓ | @next/next/no-head-element | 防止使用 <head> 元素。 |
| ✓ | @next/next/no-head-import-in-document | 防止在 pages/_document.js 中使用 next/head。 |
| ✓ | @next/next/no-html-link-for-pages | 防止使用 <a> 元素导航到 Next.js 内部页面。 |
| ✓ | @next/next/no-img-element | 由于 LCP 更慢且带宽占用更高,防止使用 <img> 元素。 |
| ✓ | @next/next/no-page-custom-font | 防止使用仅作用于页面的自定义字体。 |
| ✓ | @next/next/no-script-component-in-head | 防止在 next/head 组件中使用 next/script。 |
| ✓ | @next/next/no-styled-jsx-in-document | 防止在 pages/_document.js 中使用 styled-jsx。 |
| ✓ | @next/next/no-sync-scripts | 防止使用同步脚本。 |
| ✓ | @next/next/no-title-in-document-head | 防止将 <title> 与来自 next/document 的 Head 组件一起使用。 |
| ✓ | @next/next/no-typos | 防止在 Next.js 的数据获取函数 中出现常见拼写错误。 |
| ✓ | @next/next/no-unwanted-polyfillio | 防止来自 Polyfill.io 的重复 polyfill。 |
我们建议在开发期间使用合适的集成,以便直接在代码编辑器中查看警告和错误。
移除 next lint
从 Next.js 16 开始,next lint 已被移除。
作为移除工作的一部分,Next 配置文件中的 eslint 选项已不再需要,可以安全地删除。
示例
在 monorepo 中指定根目录
如果在未将 Next.js 安装在根目录的项目(例如 monorepo)中使用 @next/eslint-plugin-next,你可以通过 eslint.config.mjs 中的 settings 属性告诉 @next/eslint-plugin-next 在哪里找到你的 Next.js 应用:
import { defineConfig } from 'eslint/config'
import eslintNextPlugin from '@next/eslint-plugin-next'
const eslintConfig = defineConfig([
{
files: ['**/*.{js,jsx,ts,tsx}'],
plugins: {
'@next/next': eslintNextPlugin,
},
settings: {
next: {
rootDir: 'packages/my-app/',
},
},
},
])
export default eslintConfig
rootDir 可以是路径(相对或绝对)、glob(即 "packages/*/"),或由路径和/或 glob 组成的数组。
禁用规则
如果你想修改或禁用受支持插件(react、react-hooks、next)提供的任何规则,可以直接使用 eslint.config.mjs 中的 rules 属性进行更改:
import { defineConfig, globalIgnores } from 'eslint/config'
import nextVitals from 'eslint-config-next/core-web-vitals'
const eslintConfig = defineConfig([
...nextVitals,
{
rules: {
'react/no-unescaped-entities': 'off',
'@next/next/no-page-custom-font': 'off',
},
},
// Override default ignores of eslint-config-next.
globalIgnores([
// Default ignores of eslint-config-next:
'.next/**',
'out/**',
'build/**',
'next-env.d.ts',
]),
])
export default eslintConfig
配合 Core Web Vitals
在 ESLint 配置中启用 eslint-config-next/core-web-vitals 配置。
import { defineConfig, globalIgnores } from 'eslint/config'
import nextVitals from 'eslint-config-next/core-web-vitals'
const eslintConfig = defineConfig([
...nextVitals,
// Override default ignores of eslint-config-next.
globalIgnores([
// Default ignores of eslint-config-next:
'.next/**',
'out/**',
'build/**',
'next-env.d.ts',
]),
])
export default eslintConfig
eslint-config-next/core-web-vitals 会将 @next/eslint-plugin-next 中的某些 lint 规则从警告升级为错误,以帮助改善你的 Core Web Vitals 指标。
使用 Create Next App 创建的新应用会自动包含
eslint-config-next/core-web-vitals配置。
配合 TypeScript
除了 Next.js ESLint 规则之外,create-next-app --typescript 还会通过 eslint-config-next/typescript 向你的配置添加 TypeScript 专用 lint 规则:
import { defineConfig, globalIgnores } from 'eslint/config'
import nextVitals from 'eslint-config-next/core-web-vitals'
import nextTs from 'eslint-config-next/typescript'
const eslintConfig = defineConfig([
...nextVitals,
...nextTs,
// Override default ignores of eslint-config-next.
globalIgnores([
// Default ignores of eslint-config-next:
'.next/**',
'out/**',
'build/**',
'next-env.d.ts',
]),
])
export default eslintConfig
这些规则基于 plugin:@typescript-eslint/recommended。
详见 typescript-eslint > Configs。
配合 TypeScript 7
使用 TypeScript 7 时,eslint-config-next 仍需要 TypeScript 6 编译器 API 来支持 typescript-eslint。直接将 TypeScript 7 安装为 typescript 会导致 ESLint 配置加载失败。
使用 npm 时,请在 package.json 中使用这些别名,并运行 npm install:
{
"devDependencies": {
"@typescript/native": "npm:typescript@7.0.2",
"typescript": "npm:@typescript/typescript6@6.0.2"
}
}
这样便可在 tsc 运行 TypeScript 7 的同时,为 lint 工具提供 TypeScript 6 API。tsc6 命令会运行 TypeScript 6。
有关对 TypeScript 7 的原生支持,请参阅微软的并行设置说明以及 typescript-eslint 跟踪 issue。
配合 Prettier
ESLint 也包含代码格式化规则,这些规则可能与现有的 Prettier 配置冲突。我们建议在 ESLint 配置中加入 eslint-config-prettier,让 ESLint 与 Prettier 协同工作。
首先,安装该依赖:
pnpm
pnpm add -D eslint-config-prettier
npm
npm i -D eslint-config-prettier
yarn
yarn add --dev eslint-config-prettier
bun
bun add -d eslint-config-prettier
然后,将 prettier 添加到现有的 ESLint 配置中:
import { defineConfig, globalIgnores } from 'eslint/config'
import nextVitals from 'eslint-config-next/core-web-vitals'
import prettier from 'eslint-config-prettier/flat'
const eslintConfig = defineConfig([
...nextVitals,
prettier,
// Override default ignores of eslint-config-next.
globalIgnores([
// Default ignores of eslint-config-next:
'.next/**',
'out/**',
'build/**',
'next-env.d.ts',
]),
])
export default eslintConfig
对暂存文件运行 lint
如果你希望配合 lint-staged 使用 ESLint,对已暂存的 git 文件运行 linter,请在项目根目录的 .lintstagedrc.js 文件中添加以下内容:
const path = require('path')
const buildEslintCommand = (filenames) =>
`eslint --fix ${filenames
.map((f) => `"${path.relative(process.cwd(), f)}"`)
.join(' ')}`
module.exports = {
'*.{js,jsx,ts,tsx}': [buildEslintCommand],
}
迁移现有配置
如果你的应用中已配置 ESLint,则有两种方式集成 Next.js 的 linting 规则,具体取决于你的设置。
直接使用插件
如果你已配置以下任一项,请直接使用 @next/eslint-plugin-next:
- 单独安装或通过其他配置(例如
airbnb或react-app)安装了冲突的插件:reactreact-hooksjsx-a11yimport
- 自定义的
parserOptions与 Next.js 默认值不同(仅当你已自定义 Babel 配置时) eslint-plugin-import使用了自定义的 Node.js 和/或 TypeScript 解析器
在这些情况下,直接使用 @next/eslint-plugin-next 以避免冲突:
首先,安装该插件:
pnpm
pnpm add -D @next/eslint-plugin-next
npm
npm i -D @next/eslint-plugin-next
yarn
yarn add --dev @next/eslint-plugin-next
bun
bun add -d @next/eslint-plugin-next
然后将其添加到你的 ESLint 配置中:
import { defineConfig } from 'eslint/config'
import nextPlugin from '@next/eslint-plugin-next'
const eslintConfig = defineConfig([
// Your other configurations...
{
files: ['**/*.{js,jsx,ts,tsx}'],
plugins: {
'@next/next': nextPlugin,
},
rules: {
...nextPlugin.configs.recommended.rules,
},
},
])
export default eslintConfig
这种方法消除了在多个配置中导入相同插件或解析器时可能发生的冲突或错误风险。
添加到现有配置
如果你要将 Next.js 添加到现有的 ESLint 设置中,请将 Next.js 配置展开到你的数组中:
import nextConfig from 'eslint-config-next/core-web-vitals'
// Your other config imports...
const eslintConfig = [
// Your other configurations...
...nextConfig,
]
export default eslintConfig
当你展开 ...nextConfig 时,你添加的是多个配置对象,其中包括文件模式、插件、规则、忽略项和解析器设置。ESLint 按顺序应用配置,因此对于匹配的文件,后面的规则可以覆盖前面的规则。
须知:
这种方法适用于简单的设置。如果你有复杂的现有配置,且其特定的文件模式或插件配置存在冲突,请考虑直接使用插件(如上所示)以获得更细粒度的控制。
| 版本 | 变更 |
|---|---|
v16.4.0 |
eslint-config-next 增加了对 ESLint 10 的兼容性,包括支持默认的 React 版本检测。 |
v16.0.0 |
next lint 和 eslint next.config.js 选项已被移除,改用 ESLint CLI。可以使用 codemod 来帮助你迁移。 |