Skip to main content

Usage

note

ESLint version 8 is now deprecated, and upgrading to a newer version is recommended for continued support and improvements. Refer the migration guide to update eslint in your project with minimal changes.

To set up ESLint, create a .eslintrc, .eslintrc.js or .eslintrc.json file in your project root. Use this file to specify the configuration for your application by referring to the relevant section of this guide.

touch .eslintrc.js

Node.js​

For Node.js applications, use only the js configuration this package. This import is essential for all JavaScript-based projects, as it defines the recommended set of JavaScript-specific linting rules and ensures consistent code quality.

note

If you are using Typescript in your project, do include the ts module in your config file.

.eslintrc.js
module.exports = {
extends: [
"@nish1896/eslint-config/js",
"@nish1896/eslint-config/ts"
],
rules: {}
}

React & Vite​

The jsx import of this package contains the relevant rules for linting jsx code.

.eslintrc.js
module.exports = {
extends: [
"@nish1896/eslint-config/js",
"@nish1896/eslint-config/ts",
"@nish1896/eslint-config/jsx"
],
rules: {}
}

NextJS​

The existing react import caused conflicts with the eslint-plugin-react-hooks plugin, which prevented linting to execute.

From version 2.1.0 onwards, a separate next import has been introduced to address these conflicts, since NextJS also provides its own set of eslint configuration.

.eslintrc.js
module.exports = {
extends: [
"@nish1896/eslint-config/js",
"@nish1896/eslint-config/next-ts",
"@nish1896/eslint-config/next"
],
rules: {}
}

Add, Remove or 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

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.