Skip to main content
Version: v2

Usage

This guide explains how to use the package with ESLint version 9, detailing specific configurations for each. It provides module imports tailored for Node.js, React, Vite or Next.js applications, ensuring compatibility and streamlined integration for diverse project environments.

Starting from version 2 of @nish1896/eslint-flat-config the package has been modularized into the following independent components, enabling developers to apply rules specific to their framework and whether their project uses TypeScript:

Example applications that utilize eslint-flat-config can be viewed here.

If you have specified "type": "module" in the package.json of your application, create a eslint.config.js file, else use eslint.config.mjs file.

touch eslint.config.mjs

Node.js​

The js import extends rules from the typescript-eslint package.

eslint.config.mjs
import jsConfig from '@nish1896/eslint-flat-config/js';

export default [
...jsConfig,
{
rules: {}
}
];

With Typescript

eslint.config.mjs
import jsConfig from '@nish1896/eslint-flat-config/js';
import tsConfig from '@nish1896/eslint-flat-config/ts';

export default [
...jsConfig,
...tsConfig,
{
rules: {}
}
];

React & Vite​

Use both js and jsx rules from the package for React and Vite applications.

eslint.config.mjs
import jsConfig from '@nish1896/eslint-flat-config/js';
import jsxConfig from '@nish1896/eslint-flat-config/jsx';

export default [
...jsConfig,
...jsxConfig,
{
rules: {}
}
];

With Typescript

eslint.config.mjs
import jsConfig from '@nish1896/eslint-flat-config/js';
import tsConfig from '@nish1896/eslint-flat-config/ts';
import jsxConfig from '@nish1896/eslint-flat-config/jsx';

export default [
...jsConfig,
...tsConfig,
...jsxConfig,
{
rules: {}
}
];

Next.js​

A dedicated ESLint configuration for Next.js applications is now available from version 1.1.0 of the package. It integrates seamlessly with the create-next-app boilerplate, which sets up projects using Next.js version 15. This simplifies setup, ensuring compatibility and optimized linting rules tailored for Next.js applications.

The next and next-ts modules in this package consolidates rules from both jsx and typescript configurations, extending the respective stylistic plugin. It leverages the typescript-eslint and other React plugins provided by eslint-config-next, ensuring no need to duplicate plugin configurations for Next.js projects and to prevent the redefine plugin error.

eslint.config.mjs
import { dirname } from 'path';
import { fileURLToPath } from 'url';
import { FlatCompat } from '@eslint/eslintrc';
import jsConfig from '@nish1896/eslint-flat-config/js';
import nextConfig from '@nish1896/eslint-flat-config/next';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const compat = new FlatCompat({
baseDirectory: __dirname,
});

const eslintConfig = [
...jsConfig,
...nextConfig,
...compat.extends('next/core-web-vitals'),
{
rules: {
}
}
];

export default eslintConfig;

With Typescript

eslint.config.mjs
import { dirname } from 'path';
import { fileURLToPath } from 'url';
import { FlatCompat } from '@eslint/eslintrc';
import jsConfig from '@nish1896/eslint-flat-config/js';
import nextConfig from '@nish1896/eslint-flat-config/next';
import nextTsConfig from '@nish1896/eslint-flat-config/next-ts';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const compat = new FlatCompat({
baseDirectory: __dirname,
});

const eslintConfig = [
...jsConfig,
...nextConfig,
...nextTsConfig,
...compat.extends('next/core-web-vitals', 'next/typescript'),
{
rules: {
}
}
];

export default eslintConfig;

Duplicate Import Error​

For ESLint 9, the error "Cannot redefine plugin @typescript-eslint" or anything similar occurs because the @typescript-eslint plugin is initialized twice: once from the ts config of this package and then from your current configuration. To fix this:

  • Remove @typescript-eslint from the plugins key in your ESLint config.
  • Let the ts module of @nish1896/eslint-flat-config manage the plugin initialization.
  • Configure or turn off any rule in the rules key of eslint config.

This approach ensures no duplication while retaining flexibility for rule customization.

Modify Rules​

To add a new rule, turn off or modify the existing list of rules, append the rules in your eslint configuration file.

{
rules: {
"<new-rule>": "error",
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": "off", // for TypeScript files
"id-length": ["warn", { min: 3, max: 20 }]
}
}

To disable one or more rules throughout the file, add this at the top of your file.

/* eslint-disable @typescript-eslint/no-unused-vars, @typescript-eslint/no-explicit-any */

To disable one or more rules in the next line,

// eslint-disable-next-line <rule1>, <rule2>

or

/* eslint-disable-next-line <rule1>, <rule2> */
warning

The syntax below won't work, ESLint ignores /** */ (JSDoc) comments

/** eslint-disable-next-line @typescript-eslint/no-unused-vars */

To run linting on your codebase,

npm run lint
yarn lint
pnpm lint

For formatting non-js like .css, .html files you can use prettier alongside eslint. Prettier configuration, .prettierignore and usage of eslint with prettier in pre-commit hook can be referenced from my react-node-ts-monorepo.