Skip to content
BENIBIX Learn

Linters and Formatters

Level 2 · Checking and fixing code

How a linter warns about likely bugs, and how a formatter tidies the layout. Why ESLint and Prettier are two tools, and how to run both.

Course contentsLesson 4 of 13

After this lesson you can

  • Tell a linter from a formatter
  • Read a linter warning
  • Run Prettier on a whole project

12 mincovers 4 glossary entries

Worth reading first: Vim, Neovim and Emacs

What a linter checks, and what a formatter fixes

A linter reads your code without running it and warns you about likely mistakes. A formatter rewrites the layout of your code in one fixed style. ESLint, the most used linter for JavaScript, came out in 2013. Prettier, the most used formatter, came out in 2017. Most teams run both, each time they save.

A linter catches things like a name you set and never use, a name spelled two ways, or code that can never run. Each check is a rule, and you turn rules on or off in a settings file. Many warnings come with a fix the linter can make for you.

ESLint is free, and it works on JavaScript and TypeScript. Its settings live in a file called eslint.config.js. Each warning names the line, the column and the rule that fired, so you can look the rule up and see why it exists.

A formatter never changes what the code does, only how it looks. It sets the spaces, the line breaks, the quotes and the indents. Prettier works on JavaScript, TypeScript, CSS, HTML, JSON, Markdown, YAML and more. It has very few settings on purpose, so a team stops arguing about tabs.

The two overlap a little, and that can cause trouble. Old linter setups also had rules about spaces, which fought with the formatter. The usual fix is simple. Let the formatter own the layout, and let the linter own the bugs.

Run the formatter over the whole project once, in its own commit. After that, each change shows only real edits. If you format just the files you touch, each change mixes real edits with layout edits. Then the people who read it cannot tell which is which.

The common mistake is turning linter warnings off because there are too many. Each one is a hint about a real slip. Fix a few at a time, and switch a rule off only after you have read why it fired.

A linter and a formatter side by side

ToolWhat it doesChanges how the code runs?Example
LinterWarns about likely bugs and risky codeNo, it only reportsESLint
FormatterRewrites spaces, breaks and quotesNo, only the lookPrettier

Which one to reach for

  1. 1

    Linter

    When you want to find a bug before you run the code. One example is a name you set and never used.

  2. 2

    Formatter

    When a team keeps arguing about spaces and quotes, or a change is full of layout noise.

console
$ npx eslint app.js

/home/emma/shop/app.js
  3:7  error  'total' is assigned a value but never used  no-unused-vars

✖ 1 problem (1 error, 0 warnings)

$ npx prettier . --write
app.js 35ms
eslint.config.js 4ms
ESLint names the line, the column and the rule. Prettier then rewrites each file in the folder and lists it.
Prettier throws away the old spacing and prints the code again in one style. What the code does stays the same.

Worked example

A team stops arguing about tabs

Emma's team of 4 spends part of every code review on spaces and quotes. They add Prettier, run it on all 300 files in one commit, and set it to run on save.

They also add ESLint. In its first run it finds 12 names that are set and never used. Two of them were real bugs.

The words for this lesson4

Check yourself

How is a linter not the same as a formatter?

Finished this lesson?

Marking it complete also marks its 4 glossary entries learned, which updates your roadmap and your saved page. 0 are already marked.