How to use new "flat config" approach in Eslint? #49337
-
SummaryI have Eslint working in a TypeScript Next.js project, but I want to switch to the new "flat config" approach that Eslint offers. (Why is that not Next.js's default?) I created Part 1How can I migrate the Part 2Other than missing But my VSCode no longer shows errors. I see How can I get the flat config to run automatically in VSC? Additional informationNo response ExampleNo response |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 21 replies
-
This worked for me. I haven't tested it thoroughly, but it reports that I shouldn't use the import nextPlugin from '@next/eslint-plugin-next';
import reactPlugin from 'eslint-plugin-react';
import hooksPlugin from 'eslint-plugin-react-hooks';
export default [
{
plugins: {
react: reactPlugin,
},
rules: {
...reactPlugin.configs['jsx-runtime'].rules,
},
settings: {
react: {
version: 'detect', // You can add this if you get a warning about the React version when you lint
},
},
},
{
plugins: {
'react-hooks': hooksPlugin,
},
rules: hooksPlugin.configs.recommended.rules,
},
{
plugins: {
'@next/next': nextPlugin,
},
rules: {
...nextPlugin.configs.recommended.rules,
...nextPlugin.configs['core-web-vitals'].rules,
},
},
{
ignores: ['.next/*'],
},
]; You need to install |
Beta Was this translation helpful? Give feedback.
-
@ryancwalsh, did you consider using import path from "node:path";
import { fileURLToPath } from "url";
import { FlatCompat } from "@eslint/eslintrc";
import { defineFlatConfig } from "eslint-define-config";
import ts from "@typescript-eslint/eslint-plugin";
import tsParser from "@typescript-eslint/parser";
import js from "@eslint/js";
const baseDirectory = path.dirname(fileURLToPath(import.meta.url));
const compat = new FlatCompat({
baseDirectory,
resolvePluginsRelativeTo: baseDirectory,
});
export default defineFlatConfig([
{
files: ["**/*.{ts,tsx}"],
plugins: {
"@typescript-eslint": ts,
},
languageOptions: {
parser: tsParser,
},
rules: {
...js.configs.recommended.rules,
...ts.configs.recommended.rules,
},
},
compat.config({
overrides: [
{ files: "ui/**/*.{ts,tsx}", extends: "next/core-web-vitals" },
],
})
]); I'm having issues getting this to work, but theoretically I think this is the ideal solution. |
Beta Was this translation helpful? Give feedback.
-
EDIT again: I have created a package Just another option whilst we wait for the updated config. EDIT: After some more research, I believe the best options thus far until Next.js releases a flat config is: eslint.config.mjs // flat compat
import url from "node:url";
import eslint from "@eslint/js";
import { FlatCompat } from "@eslint/eslintrc";
const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
const compat = new FlatCompat({ baseDirectory: __dirname });
/** @type {import('eslint').Linter.FlatConfig[]} */
export default [eslint.configs.recommended, ...compat.extends("next")]; or (but you miss out on setting other eslint rules and parsing, and just get access to the default nextjs rules) import eslint from "@eslint/js";
import next from "@next/eslint-plugin-next";
/** @type {import('eslint').Linter.FlatConfig[]} */
export default [
eslint.configs.recommended,
{
plugins: {
"@next/next": next,
},
rules: {
...next.configs.recommended.rules,
},
},
]; you can also just use the legacy config and not upgrade to eslint v9, which probably is the best idea for maximum compatability (i had some issues using FlatCompat in a monorepo) Hello, sorry for reviving an answered discussion but I have discovered an (external) package that offers the Next.js rules for use in ESLint Flat config. Not affiliated with them at all -- discovered it in use on the discord.js repo (discord.js/eslint.config.js).
from ESLint Config Neon: https://github.com/iCrawl/eslint-config-neon FlatCompat is also an option |
Beta Was this translation helpful? Give feedback.
-
ESLint has just released @eslint/compat, which looks much more elegant. // eslint.config.js
import { fixupConfigRules } from "@eslint/compat";
import { FlatCompat } from "@eslint/eslintrc";
const compat = new FlatCompat();
/** @type {import("eslint").Linter.FlatConfig[]} */
const config = [
...fixupConfigRules(compat.extends("next/core-web-vitals", "prettier")),
{ ignores: [".next"] },
];
export default config; This works fine, until
|
Beta Was this translation helpful? Give feedback.
-
Wish the Vercel team prioritized this a bit higher. It's a fairly small lift but is impacting the developer experience for a lot of us. If Next is striving so aggressively to use the newest standards (RSC, server actions, etc), can we not reflect that same mindset with things as small as our ESLint configs? |
Beta Was this translation helpful? Give feedback.
-
I wrote a setup guide for Eslint 9 with Next.js detailing my approach to flat config with Next.js : Eslint 9 & Next.js — Setup Guide Also for a TLDR; check my comment here |
Beta Was this translation helpful? Give feedback.
-
For anyone who want try eslint 9 flat config in Nextjs. Make sure to backup your old See: https://eslint.org/docs/latest/use/configure/migration-guide#migrate-your-config-file npx @eslint/migrate-config .eslintrc.json This will create a Then install some packages Then run Works for me in Next.js 15. "dependencies": {
"next": "^15.0.1",
"react": "^18",
"react-dom": "^18"
},
"devDependencies": {
"@types/react": "^18",
"@types/react-dom": "^18",
"@eslint/eslintrc": "^3.1.0",
"@eslint/js": "^9.13.0",
"eslint": "^9.13.0",
"eslint-config-next": "^15.0.1",
"postcss": "^8",
"tailwindcss": "^3.4.1"
} |
Beta Was this translation helpful? Give feedback.
Hmm, it worked for me for TS and TSX files. Might be that it's missing the matcher?
We could maybe put the React and Next stuff into one object and add
files: ['**/*.ts', '**/*.tsx'],
to it.Something like this:
And yes, sorry, …