Linting with ESLint
A linter statically analyzes your code for problems without running it — likely bugs, suspect patterns, and style violations — and ESLint is the standard linter for JavaScript and TypeScript. It catches things the language won't: an unused variable, an unhandled promise, a == where you meant ===, a missing await, a forgotten break. Configured with shared rule sets and run in your editor and CI, it keeps a codebase consistent and bug-resistant across a whole team. This page covers what linting is (vs formatting), the flat config, rules and severities, autofix, and integrating ESLint into your workflow.
Linting vs formatting
Linter (ESLint) | Formatter (Prettier) | |
|---|---|---|
Concerned with | Code correctness & quality | Code appearance |
Catches | Bugs, bad patterns, unused vars, | Indentation, quotes, line width, commas |
Can change meaning? | Yes (some autofixes alter logic) | No — only whitespace/syntax style |
Example complaint | " | "Expected 2 spaces, found 4" |
Setup and flat config
npm install --save-dev eslint npx eslint --init # interactive setup: picks plugins, creates the config
eslint.config.js (flat config — the modern format)
import js from '@eslint/js'
export default [
js.configs.recommended, // ESLint's recommended baseline rules
{
files: ['**/*.js'],
languageOptions: {
ecmaVersion: 2023,
sourceType: 'module',
globals: { process: 'readonly', console: 'readonly' }, // Node globals
},
rules: {
'no-unused-vars': 'warn',
'no-console': 'off',
eqeqeq: 'error', // require === over ==
'no-await-in-loop': 'warn',
},
},
]Rules and severity levels
rules: {
'no-debugger': 'error', // 2 — fails the lint (non-zero exit) → blocks CI
'no-unused-vars': 'warn', // 1 — reported, but does NOT fail the build
'no-console': 'off', // 0 — rule disabled
}
// Disable a rule inline for a justified exception (use sparingly):
// eslint-disable-next-line no-console
console.log('intentional startup banner')Autofix and CI integration
package.json scripts
{
"scripts": {
"lint": "eslint .", // check everything (use in CI — fails on errors)
"lint:fix": "eslint . --fix" // auto-fix everything ESLint safely can
}
}