Skip to main content
Version: v1

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.

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: {}
}
];

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: {}
}
];

NextJS​

A dedicated ESLint configuration for Next.js applications is now available since 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 module in this package consolidates rules from both js and jsx configurations, extending the 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 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 = [
...nextConfig,
...compat.extends('next/core-web-vitals', 'next/typescript')
];

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 js 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 js 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.