ESLint
Next.js には、ESLint プラグイン @next/eslint-plugin-next がバンドルされており、Next.js アプリケーションで一般的な問題やエラーを検出できます。
ESLint のセットアップ
ESLint CLI (フラット設定) で lint を迅速に動作させる
-
ESLint と Next.js の設定をインストールする
ターミナルpnpm add -D eslint eslint-config-next @eslint/eslintrc -
eslint.config.mjsを Next.js の設定で作成するeslint.config.mjsimport { 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 exec 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 | クライアントコンポーネントが非同期関数になることを防ぎます。 | |
| @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/eslint-plugin-next に Next.js アプリケーションの場所を伝えることができます。
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',
},
},
// Override default ignores of eslint-config-next.
globalIgnores([
// Default ignores of 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,
// Override default ignores of eslint-config-next.
globalIgnores([
// Default ignores of 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 は TypeScript 固有の lint ルールを next/typescript とともに設定に追加します。
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 を参照してください。
Prettier とともに
ESLint にはコードフォーマットルールも含まれており、既存の Prettier セットアップと競合する可能性があります。ESLint と Prettier を連携させるために、eslint-config-prettier を ESLint 設定に含めることをお勧めします。
まず、依存関係をインストールします
pnpm 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,
// 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 の実行
Git でステージングされたファイルに対してリンターを実行するために ESLint を lint-staged と連携させたい場合は、プロジェクトのルートにある .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 を含めるのではなく、このプラグインから直接拡張することを推奨します。
推奨プラグインのルールセット
以下の条件が真である場合
- 以下のプラグインのいずれか (個別に、または
airbnbやreact-appのような別の設定経由) が既にインストールされている。reactreact-hooksjsx-a11yimport
- Next.js で Babel が構成されている方法とは異なる特定の
parserOptionsを定義した (これは、Babel 設定をカスタマイズした場合を除き、推奨されません)。 - Node.js および/または TypeScript 用の
eslint-plugin-importがインストールされており、リゾルバー がインポートを処理するために定義されている。
これらのプロパティの構成方法を好む場合は、それらの設定を削除するか、または代わりに Next.js ESLint プラグインから直接拡張することを推奨します。
module.exports = {
extends: [
//...
'plugin:@next/next/recommended',
],
}プラグインはプロジェクトに通常どおりインストールできます。
pnpm 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'],
// List of ignore patterns.
globalIgnores([]),
])
export default eslintConfignext 設定は、parser、plugins、settings プロパティのデフォルト値を既に処理しています。ユースケースで異なる設定が必要な場合を除き、これらのプロパティを個別に再宣言する必要はありません。
他の共有可能な設定を含める場合、これらのプロパティが上書きまたは変更されないようにする必要があります。そうでない場合は、next 設定と動作が重複する設定を削除するか、前述のように Next.js ESLint プラグインから直接拡張することを推奨します。
| バージョン | 変更履歴 |
|---|---|
v16.0.0 | next lint と eslint next.config.js オプションは、ESLint CLI のために削除されました。移行を支援するために、コードモッドが利用可能です。 |
役に立ちましたか?