What Is ESLint?
ESLint is the most widely used static analysis tool for JavaScript and TypeScript. Created by Nicholas C. Zakas in 2013, it analyzes your code without executing it, finding problems that range from syntax errors to style inconsistencies and potential bugs.
Unlike Prettier (which only handles formatting), ESLint checks code quality β unused variables, incorrect equality checks, security issues, and much more. When combined with its auto-fix feature, it can also rewrite many problems automatically.
ESLint vs Prettier β Key Difference
| Tool | Purpose | Can Auto-Fix? |
|---|---|---|
| ESLint | Code quality & logic errors | β Yes (many rules) |
| Prettier | Code formatting only | β Yes (formatting) |
| Both together | Full quality + style | β Best practice |
Most Important ESLint Rules
Error Prevention
no-unused-varsβ Flags variables that are declared but never usedno-undefβ Catches references to undeclared variablesno-unreachableβ Finds code afterreturn/throwthat will never runno-duplicate-caseβ Detects duplicatecaselabels in switch statementsuse-before-defineβ Prevents use of variables before their declaration
Best Practices
eqeqeqβ Requires===instead of==(avoids type coercion bugs)no-varβ Disallowsvar; useletorconstinsteadprefer-constβ Suggestsconstwhen a variable is never reassignedno-evalβ Disallows dangerouseval()callsno-implicit-globalsβ Prevents accidentally creating global variablesno-consoleβ Warns aboutconsole.log()left in production codeno-debuggerβ Preventsdebuggerstatements in production
Code Style (Auto-fixable)
semiβ Enforce or disallow semicolonsquotesβ Enforce single, double, or backtick quotesindentβ Consistent indentation (2 or 4 spaces, or tabs)comma-dangleβ Trailing commas in multiline expressionsno-trailing-spacesβ Remove trailing whitespace from linesno-multiple-empty-linesβ Limit consecutive blank linesarrow-spacingβ Spaces around fat-arrow=>object-curly-spacingβ Spaces inside{ }object literals
Setting Up ESLint in Your Project
# Install
npm install --save-dev eslint
# Initialise config (interactive)
npx eslint --init
# Lint a file
npx eslint src/index.js
# Lint and auto-fix
npx eslint src/index.js --fix
# Lint entire project
npx eslint . --ext .js,.jsx,.ts,.tsx
Example .eslintrc.json
{
"env": { "browser": true, "es2022": true, "node": true },
"extends": ["eslint:recommended"],
"parserOptions": { "ecmaVersion": "latest", "sourceType": "module" },
"rules": {
"no-unused-vars": "error",
"no-console": "warn",
"eqeqeq": ["error", "always"],
"semi": ["error", "always"],
"quotes": ["warn", "single"],
"no-var": "error",
"prefer-const": "warn"
}
}
ESLint with TypeScript
For TypeScript projects, install @typescript-eslint/parser and @typescript-eslint/eslint-plugin:
npm install --save-dev @typescript-eslint/parser @typescript-eslint/eslint-plugin
# .eslintrc.json for TypeScript
{
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"],
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"]
}
Integrating ESLint in Your Workflow
- VS Code: Install the ESLint extension β errors appear inline as you type
- Pre-commit hooks: Use
lint-staged+huskyto lint before every commit - CI/CD: Run
npx eslint . --max-warnings 0to fail builds with any warnings - Vite/Webpack: Use
vite-plugin-eslintoreslint-webpack-pluginfor build-time linting