🔧 ESLint#
A static code analysis tool for JavaScript and TypeScript. Catches bugs, enforces code style, and flags bad patterns — before you run the code.
📦 Install#
# Install locally in a project (recommended)
npm install --save-dev eslint
# Initialize config (interactive setup)
npx eslint --init⚠️ As of ESLint v9, the config format changed from
.eslintrc.*toeslint.config.js(flat config). Most tutorials online still show the old format — check the version.
✅ Core Use Cases#
- Catch bugs early (undefined variables, unused imports, unreachable code)
- Enforce consistent code style across a team
- Integrate with VS Code for real-time red underlines
- Run in CI/CD to block bad code from merging
- Works with JavaScript, TypeScript, JSX, TSX, Vue, etc.
🔑 Key Commands / Usage#
Lint a file or folder#
npx eslint src/
npx eslint src/index.jsWhat it does: Scans files and prints all rule violations with line numbers.
Auto-fix fixable issues#
npx eslint src/ --fixWhat it does: Automatically fixes issues it can (formatting, unused vars in some cases). Does NOT fix everything — complex issues still need manual fixes.
Lint only specific file types#
npx eslint src/ --ext .js,.ts,.tsxIgnore files#
Create a .eslintignore file:
node_modules/
dist/
build/
*.min.jsIn v9 flat config, ignores go inside
eslint.config.jsinstead.
Check your config is working#
npx eslint --print-config src/index.jsWhat it does: Dumps the resolved config for a specific file — useful for debugging why a rule isn’t applying.
⚙️ Config / Setup#
Old format (v8 and below) — .eslintrc.json#
{
"env": { "browser": true, "es2021": true },
"extends": ["eslint:recommended"],
"rules": {
"no-unused-vars": "warn",
"no-console": "off"
}
}New format (v9+) — eslint.config.js#
import js from "@eslint/js";
export default [
js.configs.recommended,
{
rules: {
"no-unused-vars": "warn",
"no-console": "off"
}
}
];VS Code integration#
Install the ESLint extension (dbaeumer.vscode-eslint) and add to .vscode/settings.json:
{
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit"
}
}This auto-fixes on every save.
⚠️ Gotchas & Notes#
- v8 vs v9 config format — breaking change. If you see
.eslintrc.jsin a project, it’s using old format.eslint.config.js= new flat config. --fixonly fixes “fixable” rules — not all violations can be auto-fixed- ESLint is for logic/style rules. Use Prettier separately for pure formatting (spaces, line width). They can conflict — use
eslint-config-prettierto disable formatting rules in ESLint when using both together. - TypeScript needs extra setup:
@typescript-eslint/parser+@typescript-eslint/eslint-plugin
🔗 Related Tools#
- prettier — code formatter, often used alongside ESLint
- typescript — needs
@typescript-eslintplugin to lint.tsfiles
📎 Official Links#
- Docs: https://eslint.org/docs/latest/
- Config migration (v8 → v9): https://eslint.org/docs/latest/use/configure/migration-guide
- VS Code Extension: https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint