コンテンツにスキップ

ESLint

Next.js には、ESLint プラグイン @next/eslint-plugin-next がバンドルされており、Next.js アプリケーションで一般的な問題やエラーを検出できます。

ESLint のセットアップ

ESLint CLI (フラット設定) で lint を迅速に動作させる

  1. ESLint と Next.js の設定をインストールする

    ターミナル
    pnpm add -D eslint eslint-config-next @eslint/eslintrc
  2. eslint.config.mjs を 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
  3. ESLint を実行する

    ターミナル
    pnpm exec eslint .

リファレンス

以下の ESLint プラグインからの推奨ルールセットはすべて eslint-config-next で使用されます。

ルール

すべてのルールのセットは以下のとおりです。

推奨設定で有効ルール説明
@next/next/google-font-displayGoogle Fonts の font-display の動作を強制します。
@next/next/google-font-preconnectGoogle Fonts で preconnect が使用されていることを確認します。
@next/next/inline-script-idインラインコンテンツを持つ next/script コンポーネントに id 属性を強制します。
@next/next/next-script-for-gaGoogle Analytics のインラインスクリプトを使用する場合、next/script コンポーネントを優先します。
@next/next/no-assign-module-variablemodule 変数への代入を防ぎます。
@next/next/no-async-client-componentクライアントコンポーネントが非同期関数になることを防ぎます。
@next/next/no-before-interactive-script-outside-documentpages/_document.js の外で next/scriptbeforeInteractive 戦略を使用することを防ぎます。
@next/next/no-css-tags手動のスタイルシートタグを防ぎます。
@next/next/no-document-import-in-pagepages/_document.js 以外での next/document のインポートを防ぎます。
@next/next/no-duplicate-headpages/_document.js での <Head> の重複使用を防ぎます。
@next/next/no-head-element<head>要素の使用を禁止します。
@next/next/no-head-import-in-documentpages/_document.js での next/head の使用を防ぎます。
@next/next/no-html-link-for-pages内部 Next.js ページへのナビゲーションに <a> 要素を使用することを防ぎます。
@next/next/no-img-elementLCP の低下と帯域幅の増加のため、<img> 要素の使用を防ぎます。
@next/next/no-page-custom-fontページ固有のカスタムフォントを防ぎます。
@next/next/no-script-component-in-headnext/headコンポーネントでのnext/scriptの使用を避けてください。
@next/next/no-styled-jsx-in-documentpages/_document.js での styled-jsx の使用を防ぎます。
@next/next/no-sync-scripts同期スクリプトを防ぎます。
@next/next/no-title-in-document-headnext/documentHead コンポーネントでの <title> の使用を防ぎます。
@next/next/no-typosNext.js のデータ取得関数における一般的なスペルミスを防ぎます。
@next/next/no-unwanted-polyfillioPolyfill.io からの重複したポリフィルを防ぎます。

開発中にコードエディタで直接警告やエラーを表示するために、適切な統合の使用をお勧めします。

next lint の削除

Next.js 16 から、next lint は削除されました。

削除の一環として、Next 設定ファイル内の eslint オプションは不要になり、安全に削除できます。

モノレポ内でのルートディレクトリの指定

Next.js がルートディレクトリにインストールされていないプロジェクト (例: モノレポ) で @next/eslint-plugin-next を使用している場合、eslint.config.mjssettings プロパティを使用して、@next/eslint-plugin-next に Next.js アプリケーションの場所を伝えることができます。

eslint.config.mjs
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 eslintConfig

rootDir は、パス (相対または絶対)、グロブ (例: "packages/*/")、またはパスおよび/またはグロブの配列にできます。

ルールの無効化

サポートされているプラグイン (react, react-hooks, next) によって提供されるルールを変更または無効にしたい場合は、eslint.config.mjsrules プロパティを使用して直接変更できます。

eslint.config.mjs
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 設定で拡張することにより、next/core-web-vitals ルールセットを有効にします。

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

next/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 とともに設定に追加します。

eslint.config.mjs
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 を追加します。

eslint.config.mjs
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 ファイルに以下を追加してください。

.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 を含めるのではなく、このプラグインから直接拡張することを推奨します。

以下の条件が真である場合

  • 以下のプラグインのいずれか (個別に、または airbnbreact-app のような別の設定経由) が既にインストールされている。
    • react
    • react-hooks
    • jsx-a11y
    • import
  • 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 を含めたい場合は、他の設定の後に最後に拡張されるようにしてください。例:

eslint.config.mjs
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 eslintConfig

next 設定は、parserpluginssettings プロパティのデフォルト値を既に処理しています。ユースケースで異なる設定が必要な場合を除き、これらのプロパティを個別に再宣言する必要はありません。

他の共有可能な設定を含める場合、これらのプロパティが上書きまたは変更されないようにする必要があります。そうでない場合は、next 設定と動作が重複する設定を削除するか、前述のように Next.js ESLint プラグインから直接拡張することを推奨します。

バージョン変更履歴
v16.0.0next linteslint next.config.js オプションは、ESLint CLI のために削除されました。移行を支援するために、コードモッドが利用可能です。