ESLint
1. What is ESLint?
- Linter: Identifies problematic patterns in JavaScript/TypeScript code.
- Customizable: Allows users to define rules or extend existing configurations.
- Pluggable: Supports plugins for additional rules and integration with frameworks.
- Error Prevention: Detects errors like unused variables, incorrect syntax, or non-standard coding practices.
- Code Style Enforcement: Enforces coding standards for consistency across a project.
2. Key Features
- Custom Rules: Define custom linting rules to meet your team’s requirements.
- Plugin Support: Extend functionality with plugins (e.g., React, TypeScript, Prettier).
- Configurable: Supports multiple configuration formats (
.eslintrc
files orpackage.json
). - Error Levels: Classify issues as:
off
→ Rule disabledwarn
→ Shows a warningerror
→ Fails the linting process
- Fixes: Supports automatic fixing of certain issues via the
--fix
flag.
3. Installation
Install Globally (Optional)
For global use across projects:
npm install -g eslint
Install Locally (Recommended)
For project-specific use:
npm install eslint --save-dev
4. Configuration
ESLint requires a configuration file to operate. The configuration determines which rules are enabled, disabled, or customized.
Common Configuration File Types
.eslintrc.json
(JSON format).eslintrc.js
(JavaScript format).eslintrc.yml
(YAML format)package.json
(inside theeslintConfig
field)
Create a Configuration
You can create an ESLint configuration file interactively:
npx eslint --init
This command prompts you to choose: 1. Type of Project (e.g., Node.js, React). 2. Language (JavaScript/TypeScript). 3. Styling Rules (e.g., Airbnb, StandardJS, Prettier).
Basic Example Configuration
.eslintrc.json
{
"env": {
"browser": true,
"es2021": true
},
"extends": [
"eslint:recommended",
"plugin:react/recommended"
],
"parserOptions": {
"ecmaVersion": 12,
"sourceType": "module"
},
"rules": {
"no-unused-vars": "warn",
"eqeqeq": "error",
"quotes": ["error", "single"]
}
}
Explanation:
env
: Defines environments (e.g., browser, Node.js).extends
: Extends existing rule sets (e.g.,eslint:recommended
, React rules).parserOptions
: Configures ECMAScript version and module type.rules
: Overrides or customizes specific rules.
5. Running ESLint
Lint Files
To lint a specific file:
npx eslint file.js
Lint All Files
To lint all JavaScript files in the current directory:
npx eslint .
Fix Issues Automatically
To automatically fix fixable issues:
npx eslint . --fix
Ignore Files
Create an .eslintignore
file to specify files or directories to exclude:
node_modules/
dist/
build/
6. Using ESLint with TypeScript
Install ESLint and TypeScript Plugins:
npm install --save-dev eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin
Update Configuration:
{ "parser": "@typescript-eslint/parser", "extends": [ "eslint:recommended", "plugin:@typescript-eslint/recommended" ], "rules": { "@typescript-eslint/no-unused-vars": "warn" } }
Lint TypeScript Files:
npx eslint --ext .ts .
7. Popular ESLint Plugins
1. React
Plugin:
eslint-plugin-react
Purpose: Adds React-specific linting rules.
Install:
npm install eslint-plugin-react --save-dev
2. Prettier
Plugin:
eslint-config-prettier
Purpose: Disables ESLint rules that conflict with Prettier.
Install:
npm install eslint-config-prettier eslint-plugin-prettier --save-dev
3. Import Sorting
Plugin:
eslint-plugin-import
Purpose: Enforces import sorting and consistency.
Install:
npm install eslint-plugin-import --save-dev
8. Integrating ESLint with Prettier
Purpose:
- Use ESLint for linting.
- Use Prettier for formatting.
Install Required Packages:
npm install --save-dev eslint prettier eslint-config-prettier eslint-plugin-prettier
Update Configuration:
{
"extends": [
"eslint:recommended",
"plugin:prettier/recommended"
],
"rules": {
"prettier/prettier": "error"
}
}
Run Both:
npx eslint . --fix
npx prettier --write .
9. Using ESLint in VS Code
Install ESLint Extension
- Open VS Code.
- Search for “ESLint” in the Extensions Marketplace.
- Install the ESLint extension by Microsoft.
Configure VS Code Settings
Add the following to your settings.json
:
{
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"eslint.validate": [
"javascript",
"javascriptreact",
"typescript",
"typescriptreact"
]
}
How It Works:
source.fixAll.eslint
: Automatically fixes ESLint issues on save.eslint.validate
: Specifies file types for ESLint to validate.
10. Integrating ESLint with Pre-commit Hooks
Use Husky and lint-staged to run ESLint before committing code:
Install Husky and lint-staged:
npm install --save-dev husky lint-staged
Update
package.json
:{ "husky": { "hooks": { "pre-commit": "lint-staged" } }, "lint-staged": { "*.js": "eslint --fix" } }
Enable Husky:
npx husky install
11. Best Practices
- Use Recommended Configurations:
- Start with
eslint:recommended
or other popular presets like Airbnb or Prettier.
- Start with
- Combine with Prettier:
- Use Prettier for formatting and ESLint for linting.
- Integrate with CI/CD:
- Add ESLint checks to your CI pipeline to enforce coding standards.
- Custom Rules:
- Add rules specific to your project or team’s coding guidelines.
12. Alternatives to ESLint
- JSHint: Earlier tool for linting JavaScript, but lacks modern features.
- StandardJS: A zero-config linter with its own style guide.
- TSLint: Deprecated, but previously used for TypeScript (replaced by ESLint).
13. Example ESLint Configurations
Airbnb Style Guide
npm install eslint-config-airbnb --save-dev
{
"extends": ["airbnb"]
}
Node.js
{
"env": {
"node": true
},
"extends": ["eslint:recommended"]
}