|
| 1 | +# Next.js ESLint Configuration |
| 2 | + |
| 3 | +This document describes the Next.js ESLint plugin that was removed from the JavaScript packages configuration, and how to restore it when adding a Next.js application. |
| 4 | + |
| 5 | +## What Was Removed |
| 6 | + |
| 7 | +The Next.js ESLint plugin was removed because the current packages are Node.js libraries/packages, not Next.js applications. The plugin requires a `pages/` or `src/pages/` directory for Next.js routing, which doesn't exist in library packages. |
| 8 | + |
| 9 | +### Removed from `package.json` |
| 10 | + |
| 11 | +```json |
| 12 | +"devDependencies": { |
| 13 | + "@next/eslint-plugin-next": "^12.1.6" |
| 14 | +} |
| 15 | +``` |
| 16 | + |
| 17 | +### Removed from `.eslintrc.json` |
| 18 | + |
| 19 | +```json |
| 20 | +"extends": [ |
| 21 | + "plugin:@next/next/recommended" |
| 22 | +] |
| 23 | +``` |
| 24 | + |
| 25 | +## Why It Was Removed |
| 26 | + |
| 27 | +- The `@next/next` ESLint plugin is specifically for Next.js React applications |
| 28 | +- It includes rules like `no-html-link-for-pages` that check for proper Link usage in Next.js |
| 29 | +- These rules require a Next.js pages directory structure |
| 30 | +- Without a Next.js app, the plugin generates errors like: |
| 31 | + ``` |
| 32 | + Pages directory cannot be found at .../pages or .../src/pages |
| 33 | + ``` |
| 34 | + |
| 35 | +## How to Restore for Next.js Apps |
| 36 | + |
| 37 | +When you add a Next.js application to this directory, follow these steps: |
| 38 | + |
| 39 | +### 1. Install the Next.js ESLint Plugin |
| 40 | + |
| 41 | +```bash |
| 42 | +cd packages/javascript |
| 43 | +npm install --save-dev @next/eslint-plugin-next |
| 44 | +``` |
| 45 | + |
| 46 | +Or use a specific version: |
| 47 | + |
| 48 | +```bash |
| 49 | +npm install --save-dev @next/eslint-plugin-next@^12.1.6 |
| 50 | +``` |
| 51 | + |
| 52 | +### 2. Update `.eslintrc.json` |
| 53 | + |
| 54 | +Add the Next.js plugin back to the extends array: |
| 55 | + |
| 56 | +```json |
| 57 | +{ |
| 58 | + "extends": [ |
| 59 | + "eslint:recommended", |
| 60 | + "plugin:import/errors", |
| 61 | + "plugin:import/warnings", |
| 62 | + "plugin:sonarjs/recommended", |
| 63 | + "prettier", |
| 64 | + "plugin:@next/next/recommended" |
| 65 | + ] |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +### 3. Next.js-Specific Configuration |
| 70 | + |
| 71 | +If you need to customize the Next.js ESLint rules, you can add them to the rules section: |
| 72 | + |
| 73 | +```json |
| 74 | +{ |
| 75 | + "rules": { |
| 76 | + "@next/next/no-html-link-for-pages": ["error", "path/to/your/pages/"], |
| 77 | + "@next/next/no-img-element": "error", |
| 78 | + "@next/next/no-page-custom-font": "warn" |
| 79 | + } |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +### 4. Consider Separate ESLint Configs |
| 84 | + |
| 85 | +For a monorepo with both Node.js packages and Next.js apps, consider: |
| 86 | + |
| 87 | +**Option A: Separate Config Files** |
| 88 | +- Keep the current `.eslintrc.json` for Node.js packages |
| 89 | +- Create a separate `.eslintrc.json` in your Next.js app directory with the Next.js plugin |
| 90 | + |
| 91 | +**Option B: ESLint Overrides** |
| 92 | +Use overrides in the main `.eslintrc.json`: |
| 93 | + |
| 94 | +```json |
| 95 | +{ |
| 96 | + "extends": [ |
| 97 | + "eslint:recommended", |
| 98 | + "plugin:import/errors", |
| 99 | + "plugin:import/warnings", |
| 100 | + "plugin:sonarjs/recommended", |
| 101 | + "prettier" |
| 102 | + ], |
| 103 | + "overrides": [ |
| 104 | + { |
| 105 | + "files": ["nextjs-app/**/*.{js,jsx,ts,tsx}"], |
| 106 | + "extends": ["plugin:@next/next/recommended"] |
| 107 | + } |
| 108 | + ] |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +## Next.js ESLint Rules |
| 113 | + |
| 114 | +The `@next/next/recommended` config includes rules for: |
| 115 | + |
| 116 | +- **Link usage**: Ensures proper use of Next.js `<Link>` components |
| 117 | +- **Image optimization**: Enforces use of `next/image` instead of `<img>` |
| 118 | +- **Font optimization**: Checks for proper font loading |
| 119 | +- **Script optimization**: Validates `next/script` usage |
| 120 | +- **Document structure**: Ensures proper `_document.js` setup |
| 121 | +- **Head component**: Validates Next.js `<Head>` usage |
| 122 | + |
| 123 | +## Additional Resources |
| 124 | + |
| 125 | +- [Next.js ESLint Documentation](https://nextjs.org/docs/basic-features/eslint) |
| 126 | +- [Next.js ESLint Plugin on npm](https://www.npmjs.com/package/@next/eslint-plugin-next) |
| 127 | +- [Next.js ESLint Config](https://nextjs.org/docs/app/building-your-application/configuring/eslint) |
| 128 | + |
| 129 | +## Date Removed |
| 130 | + |
| 131 | +- **Date**: October 6, 2025 |
| 132 | +- **Reason**: No Next.js applications in current package structure |
| 133 | +- **Affected Files**: `package.json`, `.eslintrc.json` |
0 commit comments