How to disable ESLint rules per line, file, or folder

ESLint is an open-source JavaScript linting utility originally created by Nicholas C. Zakas in June 2013. It serves as a powerful tool for identifying and fixing problems in your JavaScript code. ESLint statically analyzes your code to quickly find issues. It examines your JavaScript code for problematic patterns, potential bugs, and deviations from coding standards.

ESLint is highly customizable. You can configure rules, preprocess code, use custom parsers, and even write your own rules that work alongside ESLint’s built-in rules. This flexibility allows you to tailor ESLint to your specific project requirements.

To change a rule’s severity for the whole project, set the rule ID equal to one of these values in ESLint configuration files:

  • "off" or 0 - turn the rule off
  • "warn" or 1 - turn the rule on as a warning (doesn’t affect exit code)
  • "error" or 2 - turn the rule on as an error (exit code is 1 when triggered)
// eslint.config.js
export default [
  {
    rules: {
      semi: "error",
      "prefer-const": "error"
    }
  }
];

There are several scenarios where you might want to disable ESLint rules:

  • You might use third-party libraries that trigger ESLint warnings or errors.
  • You might encounter legacy code that doesn’t conform to your ESLint configuration.
  • There could be edge cases where the ESLint rule might not be applicable.
  • You might need to implement workarounds or temporary fixes.

Let’s explore how to disable ESLint rules in different scenarios:

For a single line

To disable all rules on a specific line:

console.log('foo'); // eslint-disable-line

// eslint-disable-next-line
console.log('foo');

/* eslint-disable-next-line */
console.log('foo');

console.log('foo'); /* eslint-disable-line */

To disable a specific rule on a specific line:

console.log('foo'); // eslint-disable-line no-alert

// eslint-disable-next-line no-alert
console.log('foo');

console.log('foo'); /* eslint-disable-line no-alert */

/* eslint-disable-next-line no-alert */
console.log('foo');

To disable multiple rules on a specific line:

console.log('foo'); // eslint-disable-line no-alert, quotes, semi

// eslint-disable-next-line no-alert, quotes, semi
console.log('foo');

console.log('foo'); /* eslint-disable-line no-alert, quotes, semi */

/* eslint-disable-next-line no-alert, quotes, semi */
console.log('foo');

/* eslint-disable-next-line
  no-alert,
  quotes,
  semi
*/
console.log('foo');

For part of a file

To disable rule warnings in a part of a file, use block comments in the following format:

/* eslint-disable */

console.log('hi')

/* eslint-enable */

You can also disable or enable warnings for specific rules.

/* eslint-disable rule1, rule2 */

console.log('hi');

/* eslint-enable rule1, rule2  */

For an entire file

To disable ESLint rules for an entire file, add this comment at the top of the file:

/* eslint-disable */

console.log('foo');

Alternatively, you can specify rules to enable or disable for an entire file:

/* eslint-disable no-alert */

console.log('foo');

To ensure that a rule is never applied (regardless of any future enable/disable lines):

/* eslint no-alert: "off" */

console.log('foo');

For a group of files or folder

To disable rules inside of a configuration file for a group of files, use the overrides key along with a files key. For example:

{
  "rules": {...},
  "overrides": [
    {
      "files": ["*-test.js","*.spec.js"],
      "rules": {
        "no-unused-expressions": "off"
      }
    }
  ]
}

You can tell ESLint to ignore specific files and directories by creating an .eslintignore file in your project’s root directory. The .eslintignore file is a plain text file where each line is a glob pattern indicating which paths should be omitted from linting.

build/*.js
config/*.js
bower_components/foo/*.js

If an .eslintignore file is not found and an alternate file is not specified, ESLint looks in package.json for the eslintIgnore key to check for files to ignore.

{
  "name": "mypackage",
  "version": "0.0.1",
  "eslintConfig": {
    "env": {
      "browser": true,
      "node": true
    }
  },
  "eslintIgnore": ["hello.js", "world.js"]
}