|
| 1 | +--- |
| 2 | +title: ESLint Module |
| 3 | +--- |
| 4 | + |
| 5 | +This module that generates project-aware [ESLint flat config](https://eslint.org/docs/latest/use/configure/configuration-files-new) for Nuxt. |
| 6 | + |
| 7 | +:::callout{icon="i-heroicons-light-bulb"} |
| 8 | +This module is designed for the [new ESLint flat config format](https://eslint.org/docs/latest/use/configure/configuration-files-new), which will be as default in ESLint v9. |
| 9 | +The legacy `.eslintrc` config is **not supported**. We highly recommand you to migrate over the flat config to be future-proof. If you still want to use the legacy format, you might need to manually config with [`@nuxt/eslint-config`](/packages/configs), which will also lost some features like project-aware settings. |
| 10 | +::: |
| 11 | + |
| 12 | +## Features |
| 13 | + |
| 14 | +- [ESLint flat config](https://eslint.org/docs/latest/use/configure/configuration-files-new), future-proof |
| 15 | +- Project-aware Nuxt-specific settings, [supports layers](https://nuxt.com/docs/getting-started/layers). |
| 16 | +- [Nuxt DevTools](https://github.com/nuxt/devtools) integration powered by [`eslint-flat-config-viewer`](https://github.com/antfu/eslint-flat-config-viewer) |
| 17 | + |
| 18 | +## Quick Setup |
| 19 | + |
| 20 | +```bash |
| 21 | +npm i -D @nuxt/eslint |
| 22 | +``` |
| 23 | + |
| 24 | +```ts [nuxt.config.ts] |
| 25 | +export default defineNuxtConfig({ |
| 26 | + modules: [ |
| 27 | + '@nuxt/eslint' |
| 28 | + ], |
| 29 | + eslint: { |
| 30 | + // options here |
| 31 | + } |
| 32 | +}) |
| 33 | +``` |
| 34 | + |
| 35 | +And create an `eslint.config.js` file under **your project root**, with the following content: |
| 36 | + |
| 37 | +```js [eslint.config.js] |
| 38 | +import nuxt from './.nuxt/eslint.config.mjs' |
| 39 | + |
| 40 | +export default [ |
| 41 | + ...nuxt, |
| 42 | + // your custom flat config here. |
| 43 | +] |
| 44 | +``` |
| 45 | + |
| 46 | +## Receipts |
| 47 | + |
| 48 | +### Work with VS Code |
| 49 | + |
| 50 | +Note that ESLint Flat config is not yet enabled by default in the [ESLint VS Code extension](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint), you will need to enable it via the `eslint.experimental.useFlatConfig` to get ESLint working in VS Code. (This is likely not needed after ESLint v9). |
| 51 | + |
| 52 | +```json |
| 53 | +// .vscode/settings.json |
| 54 | +{ |
| 55 | + // Enable the ESlint flat config support |
| 56 | + "eslint.experimental.useFlatConfig": true |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +### Use with Prettier |
| 61 | + |
| 62 | +This module does not enable any stylistic/formatting rules by default. You can use Prettier alongside directly. |
| 63 | + |
| 64 | +### Use with ESLint Stylistic |
| 65 | + |
| 66 | +If instead, you prefer to use ESLint for formatting as well, we also integrated [ESLint Stylistic](https://eslint.style/) to make it easy. You can opt-in by setting `config.stylistic` to `true` in the `eslint` module options. |
| 67 | + |
| 68 | +```ts [nuxt.config.ts] |
| 69 | +export default defineNuxtConfig({ |
| 70 | + modules: [ |
| 71 | + '@nuxt/eslint' |
| 72 | + ], |
| 73 | + eslint: { |
| 74 | + config: { |
| 75 | + stylistic: true // <--- |
| 76 | + } |
| 77 | + } |
| 78 | +}) |
| 79 | +``` |
| 80 | + |
| 81 | +You can also pass an object to customize the rules: |
| 82 | + |
| 83 | +```ts [nuxt.config.ts] |
| 84 | +export default defineNuxtConfig({ |
| 85 | + modules: [ |
| 86 | + '@nuxt/eslint' |
| 87 | + ], |
| 88 | + eslint: { |
| 89 | + config: { |
| 90 | + stylistic: { |
| 91 | + indent: 'tab', |
| 92 | + semi: true, |
| 93 | + // ... |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | +}) |
| 98 | +``` |
| 99 | + |
| 100 | +Learn more about all the available options in the [ESLint Stylistic documentation](https://eslint.style/guide/config-presets#configuration-factory). |
| 101 | + |
| 102 | +### Dev Server Checker |
| 103 | + |
| 104 | +// TODO: |
| 105 | + |
| 106 | +### Use with Custom Config Presets |
| 107 | + |
| 108 | +By default, this module installs the JS, TS and Vue plugins with their recommended rules. This might already been covered by your config presets. You can disable the default setup by disable `standalone` option. |
| 109 | + |
| 110 | +```ts [nuxt.config.ts] |
| 111 | +export default defineNuxtConfig({ |
| 112 | + modules: [ |
| 113 | + '@nuxt/eslint' |
| 114 | + ], |
| 115 | + eslint: { |
| 116 | + config: { |
| 117 | + standalone: false // <--- |
| 118 | + } |
| 119 | + } |
| 120 | +}) |
| 121 | +``` |
| 122 | + |
| 123 | +This will make this module only generate the Nuxt-specific rules and disables, so that you can merge it with your own config presets. |
| 124 | + |
| 125 | +For example, with [`@antfu/eslint-config`](https://github.com/antfu/eslint-config): |
| 126 | + |
| 127 | +```js |
| 128 | +// eslint.config.js |
| 129 | +import antfu from '@antfu/eslint-config' |
| 130 | +import nuxt from './.nuxt/eslint.config.mjs' |
| 131 | + |
| 132 | +export default antfu( |
| 133 | + { |
| 134 | + // ...@antfu/eslint-config options, |
| 135 | + }, |
| 136 | + // Add the Nuxt rules |
| 137 | + nuxt, |
| 138 | + // ...your other rules |
| 139 | +) |
| 140 | +``` |
0 commit comments