ESLint
Next.jsは@next/eslint-plugin-nextというESLintプラグインを提供しており、基本設定にすでにバンドルされているため、Next.jsアプリケーション内の一般的な問題を検出することが可能です。
ESLintのセットアップ
ESLint CLI(フラット設定)を使用して、すばやくLintを実行できます:
-
ESLintとNext.js設定をインストールします:
pnpm add -D eslint eslint-config-nextnpm i -D eslint eslint-config-nextyarn add --dev eslint eslint-config-nextbun add -d eslint eslint-config-next -
Next.js設定を使用して
eslint.config.mjsを作成します:eslint.config.mjsimport { defineConfig, globalIgnores } from 'eslint/config' import nextVitals from 'eslint-config-next/core-web-vitals' const eslintConfig = defineConfig([ ...nextVitals, // eslint-config-nextのデフォルト無視設定をオーバーライドします。 globalIgnores([ // eslint-config-nextのデフォルト無視設定: '.next/**', 'out/**', 'build/**', 'next-env.d.ts', ]), ]) export default eslintConfig -
ESLintを実行します:
pnpm exec eslint .npx eslint .yarn eslint .bunx eslint .
リファレンス
以下のESLintプラグインの推奨ルールセットはすべてeslint-config-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 | Client Componentが非同期関数になることを防止します。 | |
| @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 | Next.jsの内部ページに移動するための<a>要素の使用を防止します。 | |
| @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 | next/documentからHeadコンポーネントを使用した<title>の使用を防止します。 | |
| @next/next/no-typos | Next.jsのデータ取得関数における一般的なタイプミスを防止します。 | |
| @next/next/no-unwanted-polyfillio | Polyfill.ioからの重複ポリフィルを防止します。 |
開発中に、コードエディタで警告とエラーを直接表示するために、適切な統合を使用することをお勧めします。
next lintの削除
Next.js 16以降、next lintは削除されました。
削除の一部として、Next.設定ファイルのeslintオプションは不要になり、安全に削除できます。
例
モノレポ内のルートディレクトリを指定する
Next.jsがルートディレクトリにインストールされていないプロジェクト(モノレポなど)で@next/eslint-plugin-nextを使用している場合、eslint.config.mjsのsettingsプロパティを使用して、Next.jsアプリケーションの場所を@next/eslint-plugin-nextに伝えることができます:
import { defineConfig } from 'eslint/config'
import eslintNextPlugin from '@next/eslint-plugin-next'
const eslintConfig = defineConfig([
{
plugins: {
next: eslintNextPlugin,
},
settings: {
next: {
rootDir: 'packages/my-app/',
},
},
files: [
// ...files
],
ignores: [
// ...ignores
],
},
])
export default eslintConfigrootDirは、パス(相対パスまたは絶対パス)、グロブ(例:"packages/*/")、またはパスやグロブの配列を指定できます。
ルールの無効化
サポートされているプラグイン(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',
},
},
// eslint-config-nextのデフォルト無視設定をオーバーライドします。
globalIgnores([
// eslint-config-nextのデフォルト無視設定:
'.next/**',
'out/**',
'build/**',
'next-env.d.ts',
]),
])
export default eslintConfigCore Web Vitalsを使用する
ESLint設定でnext/core-web-vitalsルールセットを拡張して有効にします。
import { defineConfig, globalIgnores } from 'eslint/config'
import nextVitals from 'eslint-config-next/core-web-vitals'
const eslintConfig = defineConfig([
...nextVitals,
// eslint-config-nextのデフォルト無視設定をオーバーライドします。
globalIgnores([
// eslint-config-nextのデフォルト無視設定:
'.next/**',
'out/**',
'build/**',
'next-env.d.ts',
]),
])
export default eslintConfignext/core-web-vitalsは、@next/eslint-plugin-nextを更新して、Core Web Vitalsに影響を与える多数のルールについて、デフォルトでは警告であるものをエラーに変更します。
next/core-web-vitalsエントリポイントは、Create Next Appで構築された新しいアプリケーションに自動的に含まれます。
TypeScriptを使用する
Next.js ESLintルールに加えて、create-next-app --typescriptは、設定に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,
// eslint-config-nextのデフォルト無視設定をオーバーライドします。
globalIgnores([
// eslint-config-nextのデフォルト無視設定:
'.next/**',
'out/**',
'build/**',
'next-env.d.ts',
]),
])
export default eslintConfigこれらのルールはplugin:@typescript-eslint/recommendedに基づいています。詳細については、typescript-eslint > Configsを参照してください。
Prettierを使用する
ESLintには、既存のPrettierセットアップと競合する可能性のあるコードフォーマットルールも含まれています。ESLintとPrettierが連携するようにするために、ESLint設定にeslint-config-prettierを含めることをお勧めします。
まず、依存関係をインストールします:
pnpm add -D eslint-config-prettiernpm i -D eslint-config-prettieryarn add --dev eslint-config-prettierbun add -d eslint-config-prettierその後、既存のESLint設定にprettierを追加します:
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,
// eslint-config-nextのデフォルト無視設定をオーバーライドします。
globalIgnores([
// 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が設定されている場合は、いくつかの条件が満たされていない限り、eslint-config-nextを含める代わりに、このプラグインから直接拡張することをお勧めします。
推奨プラグインルールセット
以下の条件が当てはまる場合:
- 次のプラグインの1つ以上がすでにインストールされている(別途またはairbnbやreact-appなどの別の設定を通じて):
reactreact-hooksjsx-a11yimport
- Next.js内でBabelがどのように設定されているかとは異なる特定の
parserOptionsを定義している(Babel設定をカスタマイズしていない限りお勧めしません) - Node.jsまたはTypeScriptリゾルバーで
eslint-plugin-importがインストールされており、インポートを処理するように定義されている
その場合は、eslint-config-next内でこれらのプロパティがどのように設定されているかの方法を好む場合は、これらの設定を削除するか、代わりにNext.js ESLintプラグインから直接拡張することをお勧めします:
module.exports = {
extends: [
//...
'plugin:@next/next/recommended',
],
}プラグインはプロジェクトで通常どおりインストールできます:
pnpm add -D @next/eslint-plugin-nextnpm i -D @next/eslint-plugin-nextyarn add --dev @next/eslint-plugin-nextbun add -d @next/eslint-plugin-nextこれにより、複数の設定にわたって同じプラグインまたはパーサーをインポートすることにより発生する可能性のある競合またはエラーのリスクが軽減されます。
追加設定
すでに別のESLint設定を使用していて、eslint-config-nextを含めたい場合は、他の設定の後に最後に拡張されることを確認してください。例えば:
import { defineConfig, globalIgnores } from 'eslint/config'
import nextPlugin from '@next/eslint-plugin-next'
const eslintConfig = defineConfig([
nextPlugin.configs['core-web-vitals'],
// 無視パターンのリスト。
globalIgnores([]),
])
export default eslintConfignext設定は、parser、plugins、settingsプロパティのデフォルト値の設定をすでに処理しています。ユースケースに異なる設定が必要な場合を除き、これらのプロパティを手動で再度宣言する必要はありません。
他の共有可能な設定を含める場合、これらのプロパティが上書きされたり変更されたりしないようにする必要があります。そうでなければ、next設定と動作を共有する設定を削除するか、上記で説明したように直接Next.js ESLintプラグインから拡張することをお勧めします。
| バージョン | 変更 |
|---|---|
v16.0.0 | next lintとnext.config.jsのeslintオプションはESLint CLIの優先で削除されました。移行を支援するためのcodemodが利用可能です。 |