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:
js: Eslint and stylistic rules for javascriptts: TypeScript-specific rules, including stylistic and typescript-eslintjsx: Rules for React, React Hooks, JSX A11y, and stylistic JSXnext: Includes additional JSX rules along with the ones fromeslint-plugin-nextpackagenext-ts: Typescript-eslint and Stylistic-ts rules tailored for Next.js apps.
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.
import jsConfig from '@nish1896/eslint-flat-config/js';
export default [
...jsConfig,
{
rules: {}
}
];
With Typescript
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.
import jsConfig from '@nish1896/eslint-flat-config/js';
import jsxConfig from '@nish1896/eslint-flat-config/jsx';
export default [
...jsConfig,
...jsxConfig,
{
rules: {}
}
];
With Typescript
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.
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
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-eslintfrom the plugins key in your ESLint config. - Let the
tsmodule of @nish1896/eslint-flat-config manage the plugin initialization. - Configure or turn off any rule in the
ruleskey 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> */
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.