ESLint statically analyzes your code to find problems. Run ESLint as part of your continuous integration pipeline.
- Install ESLint in your project
npm install --save-dev eslint @eslint/js
- Add
eslint.config.js
configuration file.
# Create JavaScript configuration file
touch eslint.config.js
- Add configuration to the
eslint.config.js
file. See Configure ESLint documentation.
// ./eslint.config.js
import js from "@eslint/js";
export default [
js.configs.recommended,
{
rules: {
"no-unused-vars": "warn",
"no-undef": "warn"
}
}
];
The following are valid configuration file names:
eslint.config.js
eslint.config.mjs
eslint.config.cjs
export default [
{
rules: {
eqeqeq: "off",
"no-unused-vars": "error",
"prefer-const": ["error", { "ignoreReadBeforeAssign": true }]
}
}
];
By default, ESLint ignores files using the following patter: ["**/node_modules/", ".git/"]
To ignore files (added to the previous pattern):
- On the command-line, use
--ignore-pattern
- Inside your config files
// eslint.config.js
import js from '@eslint/js';
import globals from "globals";
export default [
js.configs.recommended,
{
languageOptions: {
// Set common globals (windows,document...) to prevent "no-undef"
globals: {
...globals.browser,
}
},
ignores: ['node_modules', 'vendor', '.gitlab-ci-local'],
}];
Use file comments to dynamically configure rules.
/* eslint eqeqeq: "off", curly: "error" */
To include option settings:
/* eslint quotes: ["error", "double"], curly: 2 */
Add specific descriptions too.
/* eslint eqeqeq: "off", curly: "error"
* --------
* This will not work due to the line above starting with a '*' character.
*/
- Use the command:
npx eslint
- Add a script helper to
package.json
"scripts": {
"lint:js": "eslint",
"lint:js:fix": "eslint --fix"
},
Rules have 3 configurable states:
off
/0
- turn the rule off.warn
/1
- turn the rule on, but do not throw errors.error
/2
- turn the rule on, and return an non-zero exit code.
Rule list groups rules as:
ESLint extracted formatter rules into separate packages.
Migrated plugins:
eslint
->@stylistic/eslint-plugin-js
Built-in stylistic rules for JavaScript@typescript-eslint/eslint-plugin
->@stylistic/eslint-plugin-ts
Stylistic rules for TypeScripteslint-plugin-react
->@stylistic/eslint-plugin-jsx
Stylistic rules for framework-agnostic JSX
Plugins:
@stylistic/eslint-plugin-plus
Other rules introduced by ESLint Stylistic
Unified plugins:
@stylistic/eslint-plugin
All rules from the above 4 plugins
Homepage: eslint-plugin-compa
t
Lint the browser compatibility of your code.
Homepage: eslint-plugin-cypress
Lint the Cypress tests.
Homepage: eslint-plugin-html
Lint and fix inline scripts contained in HTML files.
Homepage: eslint-plugin-jest
Rules and fixes for Jest tests.
Homepage: eslint-plugin-jsdoc
JSDoc linting rules for ESLint.
Homepage: eslint-plugin-json
ESLint plugin for JSON files.
Homepage: eslint-plugin-promise
Enforce best practices for JavaScript promises.
Homepage: eslint-plugin-unicorn
More than 100 powerful ESLint rules.
Homepage: eslint-plugin-vue
Official plugin for Vue.
Homepage: https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint
{
"editor.formatOnSave": false,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "always"
},
"eslint.enable": true
}