


# npm の場合 npx create-next-app next-ts-eslint-prettier --typescript # yarn の場合 yarn create next-app next-ts-eslint-prettier --typescript
├── .eslintrc ├── .gitignore ├── README.md ├── next-env.d.ts ├── next.config.js ├── node_modules ├── package.json ├── pages ├── public ├── styles ├── tsconfig.json └── yarn.lock
// package.json { ... "devDependencies": { "eslint": "7.30.0", "eslint-config-next": "11.0.1", } ... }
.eslintrc は ESLint の設定ファイルを意味しており、デフォルトで eslint-config-next の設定が適用されています(ルールの詳細は後述)// .eslintrc { "extends": ["next", "next/core-web-vitals"] }
lint コマンドを実行することで、構文解析ができますが、VS Code 上でリアルタイムに構文解析することもできます。方法は至ってシンプルで、VS Code で ESLint の拡張機能をインストールするだけです!(下図参照)
sample/index.tsx というファイルを作成してください。コードは以下の通りです。// sample/index.tsx export default function index() { return ( <div> <img src="/vercel.svg" alt="Vercel Logo" width={72} height={16} /> <a href="/">test</a> </div> ); }

① Do not use img. Use Image from 'next/image' instead.
② Do not use the HTML a tag to navigate to /. Use Link from 'next/link' instead.
img タグは next/image の Image、a タグは next/link の Link を使用するようなエラーとなっています。lint コマンドを使用します。# npm の場合 npm run lint # yarn の場合 yarn lint
# 実行結果 $ next lint info - Using webpack 5. Reason: Enabled by default https://nextjs.org/docs/messages/webpack5 ✔ No ESLint warnings or errors ✨ Done in 1.50s.

pageslibcomponentssrc ディレクトリに pages 含むソースコードを管理。lint コマンドのオプションで --dir src とすることで、src ディレクトリ配下にある指定ファイルが ESLint の対象となるようにしています。src ディレクトリを作成し、以下 3 つのディレクトリを管理しましょう。pagessamplestylessrc ├── pages │ ├── _app.tsx │ ├── api │ └── index.tsx ├── sample │ └── index.tsx └── styles ├── Home.module.css └── globals.css
package.json の lint コマンドに対し、--dir src を記述してください。// package.json "scripts": { ... "lint": "next lint --dir src" ... },
lint コマンドを実行し、VS Code 上のエラーと同様の結果が表示されれば成功です!
tsconfig.json で src ディレクトリを baseURL として設定することもオススメです。
参考:【Next.js】特定のディレクトリを基準にし、絶対パスでモジュールをインポートする方法 | fwywd(フュード)// package.json "scripts": { ... "lint": "next lint --dir src", // 追記 // src --ext .js,jsx,.ts,.ts: src ディレクトリ内の js, jsx, ts, tsx ファイルを対象 // --fix: コード整形 "lint:fix": "eslint src --ext .js,jsx,.ts,.tsx --fix" ... },
.eslintrc のように、設定ファイルを記述してみましょう。// .eslintrc { "extends": ["next", "next/core-web-vitals"], // 追記 "rules": { // セミコロンない場合、エラー出力 "semi": ["error", "always"] } }
rules に記述します。詳細は ESLint の公式ページをご覧ください。pages/index.tsx)に対し、下図のようにエラーが出力されるようになります。
# npm の場合 npm run lint:fix # yarn の場合 yarn lint:fix

// .eslintrc { "extends": ["next", "next/core-web-vitals"] // 削除 // "rules": { // // セミコロンない場合、エラー出力 // "semi": ["error", "always"] // } }

.prettierrc ファイルを作成し、ルールを記述package.json にルールを記述package.json に記述しており、以下のようなルールを設定しています。// package.json "prettier": { "trailingComma": "all",// 末尾のカンマあり "tabWidth": 2,// tab の長さは半角スペース 2 つ "semi": true,// セミコロンあり "singleQuote": true,// シングルクォーテーションに統一 "jsxSingleQuote": true,//jsx もシングルクォーテーションに統一 "printWidth": 100 // 1 行の最大文字数 100 },
# npm の場合 npm install -D prettier eslint-config-prettier # yarn の場合 yarn add -D prettier eslint-config-prettier
.eslintrc に対し、以下のように記述することで、ESLint と Prettier のコード整形がバッティングしないようになります。(公式推奨)// .eslintrc { // 追記:"prettier" "extends": ["next", "next/core-web-vitals", "prettier"] }
package.json に記述しましょう。// package.json "scripts": { ... "lint": "next lint --dir src", "lint:fix": "eslint src --ext .js,jsx,.ts,.tsx --fix", // 追記 // --write: フォーマット整形 // --ignore-path .gitignore: .gitignore に含まれているファイルはコード整形の対象外 // 対象ファイルの拡張子を指定 "format": "prettier --write --ignore-path .gitignore './**/*.{js,jsx,ts,tsx,json,css}'" ... },
src/sample/index.tsx ファイルを以下のコードに差し替えてください。// src/sample/index.tsx export default function index() { return ( <div className="sample"> test </div> ) }
format コマンドを実行後、セミコロンが付き、ダブルクオーテーション -> シングルクォーテーションとなっていれば成功です!# npm の場合 npm run format # yarn の場合 yarn format

.vscode/settings.json) は以下の通りです。// .vscode/settings.json { // デフォルトのフォーマッタを prettier に設定 "editor.defaultFormatter": "esbenp.prettier-vscode", // ファイル保存時、prettier による自動フォーマット "editor.formatOnSave": true, // ファイル保存時、ESLint による自動フォーマット "editor.codeActionsOnSave": { "source.fixAll": true } }

/* * @rushstack/eslint-patch is used to include plugins as dev * dependencies instead of imposing them as peer dependencies * * https://www.npmjs.com/package/@rushstack/eslint-patch */ require('@rushstack/eslint-patch/modern-module-resolution'); module.exports = { extends: [ 'plugin:react/recommended', 'plugin:react-hooks/recommended', 'plugin:@next/next/recommended', ], plugins: ['import', 'react', 'jsx-a11y'], rules: { 'import/no-anonymous-default-export': 'warn', 'react/react-in-jsx-scope': 'off', 'react/prop-types': 'off', 'jsx-a11y/alt-text': [ 'warn', { elements: ['img'], img: ['Image'], }, ], }, ... };
eslint-plugin-next が搭載されています。ルールの詳細は公式サイトをご覧ください。eslint-config-next をベースに、ESLint のルールをカスタマイズすることをオススメします!eslint-plugin-import が推奨するルールも反映。また、モジュールにおけるインポートの順番を、アルファベットの順番にしています。// .eslintrc { "extends": [ "next", "next/core-web-vitals", "prettier", // 追記 "plugin:import/recommended", "plugin:import/warnings" ], "rules": { // import の順番をルール化 // 参考:https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/order.md "import/order": [ "error", { "alphabetize": { "order": "asc" } } ] } }

新着記事
関連記事
著者