ESLint & Prettier: The Robot Reviewers

ESLint & Prettier: The Robot Reviewers in JavaScript: Two developers argue over tabs vs. spaces and where the semicolons go.

Concept Explanation

Two developers argue over tabs vs. spaces and where the semicolons go. Meanwhile a third ships a bug because they used == instead of ===. Both problems have the same fix: let a robot handle it.

Prettier is the formatter — it reformats your code to one consistent style (spacing, quotes, line width). It doesn't care if code is correct, only that it looks uniform. ESLint is the linter — it reads your code for bugs and bad patterns ("you declared a variable but never used it", "this await does nothing"). Prettier is the tailor; ESLint is the safety inspector.


Syntax or Core Structure

// .eslintrc — a rule that bans loose equality
{
  "rules": {
    "eqeqeq": "error",        // force === over ==
    "no-unused-vars": "warn"  // nudge you about dead code
  }
}

Run them from the command line:

npx prettier --write .   # auto-format every file
npx eslint .             # report code-quality problems

Real World Example

The Pre-Commit Hook: Teams wire Prettier + ESLint into a Git hook (via Husky). Try to commit messy or buggy code and the commit is blocked until it's fixed — so main stays clean automatically.


Common Mistakes

  • Overlapping Rules: Letting ESLint and Prettier both format causes fights. Use eslint-config-prettier to turn off ESLint's style rules.
  • Ignoring Warnings: A wall of yellow warnings quickly becomes noise. Fix them or downgrade rules you truly don't want.

Best Practices

  • Format on Save: Configure your editor to run Prettier every save — you'll never think about spacing again.
  • Share the Config: Commit .eslintrc and .prettierrc so every teammate gets identical rules.

Practice Exercises

  1. The Cleanup: Take a messy file with random indentation and run prettier --write on it.
  2. The Rule Maker: Add the eqeqeq rule to ESLint and watch it flag a == in your code.
  3. The Editor Setup: Turn on "Format on Save" in your editor and confirm it reformats as you type.