Linting, Formatting & Code Quality

Intermediate · 20 min

Why ESLint and Prettier solve different problems, and how editor integration plus pre-commit hooks make code quality automatic rather than optional.

Before thisModule Bundlers & Build Tools

Traversy Media · 20 min

Linting, Formatting & Code Quality

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Traversy MediaReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A linter analyzes your code for likely bugs and risky patterns; a formatter only rewrites whitespace, quotes, and line breaks — they solve different problems and are usually used together.
  • Letting a formatter own every stylistic decision ends debates about tabs versus spaces or where to put a brace, because nobody is manually choosing anymore.
  • Editor integration that runs ESLint and Prettier on save catches problems while you're still in the file, which is far cheaper than catching them in review.
  • A pre-commit hook, usually via husky and lint-staged, re-runs linting and formatting automatically before a commit completes, so bad code structurally cannot reach the shared repository.
  • lint-staged scopes these checks to only the files actually being committed, so a large existing codebase doesn't need to pass every rule everywhere before anyone can commit anything.

Two different problems wearing one name

“Linting and formatting” gets said as one phrase so often that it’s easy to assume it’s one tool doing one job. It isn’t. A linter, like ESLint, analyzes your code’s logic and structure for patterns that are likely bugs or bad practice: a variable you declared and never used, a switch statement missing a break, comparing values with the loose == instead of ===, an async function whose rejected promise nobody handles. These are real, meaningful issues — the kind that cause production incidents.

A formatter, like Prettier, does something much narrower: it rewrites the appearance of code — indentation, quote style, where line breaks go, trailing commas — without touching its meaning at all. It has no opinion on whether your logic is correct. Running Prettier on a file with a genuine bug will faithfully reformat that bug into prettier whitespace.

They’re complementary precisely because they don’t overlap. ESLint can also check some stylistic rules, but doing that via ESLint is clunky and slower than a purpose-built formatter, so the common setup is to use Prettier for all formatting and configure ESLint to defer to it, so the two tools never fight over control of the same line.

Why a formatter earns its keep

The first time a team adopts a formatter, it feels almost pointless — “we were formatting fine by hand.” The value shows up in what it removes: the recurring, low-stakes arguments about tabs versus spaces, single versus double quotes, where an opening brace goes. None of that affects correctness, but it generates a surprising amount of debate and inconsistent pull request diffs when left to individual preference. A formatter takes all of those decisions off the table — you write code, save, and it’s reformatted into the team’s one agreed style automatically. Code review conversations then focus entirely on logic, naming, and structure, not on nitpicking whitespace.

Catching problems as early as possible

The cheapest point to fix an issue is the moment you introduce it, while you still remember exactly what you were doing. This is the case for editor integration: configuring your editor (commonly VS Code) to run ESLint and Prettier automatically — showing linting errors inline as you type, and reformatting the file on save. A missing dependency in a useEffect call gets flagged red before you even finish the function, not discovered three days later when a teammate reviewing your PR has to reconstruct the context from scratch to understand the bug.

Pre-commit hooks: making quality structural, not optional

Editor integration is powerful, but it’s also just a personal setting. It can be accidentally disabled, skipped on someone’s machine where the right extension was never installed, or ignored by someone in a hurry. If code quality depends entirely on every individual remembering to turn a feature on, it will eventually slip — and it only takes one unlinted, unformatted commit landing on main to start eroding the whole standard.

A pre-commit hook moves the check to a point nobody can skip by accident: the moment a commit is actually being made. The standard setup pairs two tools. husky manages Git hooks for the project — it wires up a script to run automatically whenever anyone runs git commit, and because the hook configuration itself is checked into the repository, every contributor gets it the moment they clone the project, with no manual setup. lint-staged is the second piece: it runs your linting and formatting commands, but scoped only to the files that are actually staged for this commit, not the entire codebase.

That scoping matters enormously in practice. Imagine adopting ESLint on a three-year-old codebase with thousands of pre-existing files that would fail a new rule. Requiring every one of those files to pass before anyone can commit anything would make the whole idea unworkable, and teams would just disable the hook. lint-staged sidesteps this: only the handful of files you touched in this commit get checked and auto-fixed. The codebase improves incrementally, file by file, as people naturally edit it, rather than needing a disruptive big-bang cleanup before the hook can be turned on at all.

Put together, a typical pre-commit hook will run Prettier and ESLint’s auto-fix against staged files, automatically correcting what can be fixed mechanically, and block the commit outright if a remaining linting error can’t be auto-fixed — forcing you to address it before the broken code ever reaches a shared branch, let alone a reviewer’s eyes.

Check yourself

  1. 01What's the actual difference in what ESLint and Prettier check for?

    ESLint analyzes code for things that can actually go wrong — an unused variable, a missing dependency in a React hook, comparing with == instead of ===. Prettier only reformats whitespace, quote style, and line length; it has no opinion on whether the logic is correct.

  2. 02Why configure your editor to run these tools automatically instead of running them manually before committing?

    Catching an issue the moment you write it, while the context is still in your head, is far cheaper to fix than discovering it minutes or days later in a pull request review, when you've moved on and have to reload the context.

  3. 03What problem do pre-commit hooks solve that editor integration alone doesn't?

    Editor integration is a convenience that any individual developer could have turned off, forgotten to install, or ignored. A pre-commit hook runs at the commit boundary itself and can actually block the commit, so quality checks can't silently be skipped by one person on the team.

  4. 04Why does lint-staged only check the files that changed, rather than the whole project?

    Enforcing every rule across an entire existing codebase before any single commit can land would make adopting linting on a real project impractical — lint-staged lets you tighten standards for new and touched code immediately without requiring a disruptive one-time cleanup of everything else first.

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark