Fixing Issues Automatically with --fix
You'll learn how to use ESLint's --fix flag to automatically correct many linting errors without manual editing.
What Is --fix?
ESLint doesn't just identify problems—it can fix many of them automatically. When you append the --fix flag to your ESLint command, it will modify your source files directly, correcting issues like inconsistent spacing, missing semicolons, incorrect indentation, and other style violations.
Think of it like a spell-checker that doesn't just underline mistakes but also has a "fix all" button. Instead of manually adjusting every spacing or formatting issue one by one, --fix handles the tedious work for you.
How to Use It
From the command line, simply add --fix to your ESLint command:
npx eslint src/ --fix
This will scan all files in the src/ directory, identify fixable issues, and automatically apply corrections. Not every rule can be auto-fixed—logical errors or ambiguous problems still require human judgment—but formatting and simple style issues are usually handled perfectly.
What Gets Fixed
The --fix flag works on safe, deterministic corrections. Examples include:
- Adding or removing semicolons
- Fixing indentation and spacing
- Correcting quote styles (single vs. double)
- Adjusting line breaks
Rules that involve logic or require decision-making (like unused variables or missing error handling) won't be auto-fixed because ESLint can't safely guess your intent.
Practical Workflow
Run ESLint without --fix first to see what's wrong, then use --fix to clean up. You can also integrate --fix into your scripts field in package.json for easy access, or combine it with version control to review changes before committing.
Key Takeaway: The --fix flag automates tedious formatting corrections, letting you focus on logic while ESLint handles style consistency.