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
Level 1 · The editor
Level 2 · Checking and fixing code
Level 3 · Trying code out
Level 4 · Other people's code
Level 5 · Building the project
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.
Which one to reach for
$ 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 4msWorked example
A team stops arguing about tabs
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.