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 unless you use an option like --max-warnings)"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:
Let’s explore how to disable ESLint rules in different scenarios:
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:
alert('foo'); // eslint-disable-line no-alert
// eslint-disable-next-line no-alert
alert('foo');
alert('foo'); /* eslint-disable-line no-alert */
/* eslint-disable-next-line no-alert */
alert('foo');To disable multiple rules on a specific line:
alert('foo') // eslint-disable-line no-alert, quotes, semi
// eslint-disable-next-line no-alert, quotes, semi
alert('foo')
alert('foo') /* eslint-disable-line no-alert, quotes, semi */
/* eslint-disable-next-line no-alert, quotes, semi */
alert('foo')
/* eslint-disable-next-line
no-alert,
quotes,
semi
*/
console.log('foo');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 */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');To disable rules for a group of files, add a config object with a files pattern and override the rules for those files.
// eslint.config.js
export default [
{
rules: {
semi: "error"
}
},
{
files: ["**/*-test.js", "**/*.spec.js"],
rules: {
"no-unused-expressions": "off"
}
}
];To ignore files and directories entirely, use the ignores property in eslint.config.js. In ESLint’s flat config format, .eslintignore and eslintIgnore in package.json are no longer supported.
// eslint.config.js
export default [
{
ignores: [
"build/**/*.js",
"config/**/*.js",
"bower_components/foo/**/*.js"
]
}
];