Sub topic 1
Linters and Formatters Solve Different Problems
People often lump ESLint and Prettier together as "code style tools" and assume they are interchangeable. They are not. A formatter decides how your code looks. A linter decides whether your code is likely to be wrong or breaks your team's rules. If you mix up the two jobs, you end up with tools that fight each other, configs nobody understands, and bugs that pass every check. This chapter separates the two jobs so the rest of the tutorial has a clear model to build on.
Prerequisites
This tutorial assumes you are comfortable with the following:
- Modern JavaScript syntax, including ES modules (
import/export) and arrow functions. - Running Node.js and npm from the command line.
- Editing
package.jsonand other JSON files by hand. - Basic Git usage, such as staging files and making commits.
You do not need any prior experience with ESLint or Prettier. You only need Node.js to run the short examples in this chapter.
Two Different Questions
Each tool answers one question about your code:
- Formatter: "Given this code, what is the one correct way to lay it out?" A formatter controls whitespace, line breaks, indentation, quotes, semicolons, and trailing commas. It never changes what the program does.
- Linter: "Does this code contain patterns that are probably bugs, or that the team has banned?" A linter reports problems such as unused variables, loose equality, unreachable code, or calls to forbidden APIs. Some of those problems can be fixed automatically. Many need a human decision.
In this tutorial, Prettier is the formatter and ESLint is the linter.
What a Formatter Does
Here is valid but messy JavaScript:
const total=[1,2,3].reduce((sum,n)=>{return sum+n},0)
console.log( total )With its default options, Prettier rewrites it as:
const total = [1, 2, 3].reduce((sum, n) => {
return sum + n;
}, 0);
console.log(total);Both versions print 6. Only the presentation changed. Prettier is opinionated on purpose. It exposes few options, so teams stop arguing about style and the diffs in code review show only real changes.
Prettier does not just tweak the spacing in your text. It parses the file into an abstract syntax tree (AST), throws away almost all of the original formatting, and prints the tree again from scratch. That is why the output stays the same no matter how the input was laid out. It keeps a few things from your original source. For example, it keeps single blank lines between statements (and collapses several into one), and it keeps an object literal expanded if you put a line break after its opening brace.
What a Linter Does
Now consider code that is already well formatted but wrong:
function findUser(users, id) {
const count = users.length;
for (const user of users) {
if (user.id == id) {
return user;
}
}
return null;
}
console.log(findUser([{ id: 1, name: "Ada" }], "1"));This runs and prints { id: 1, name: 'Ada' }, because == converts the string "1" to the number 1 before comparing. That might be what you want, or it might hide a bug where IDs arrive as strings from a URL. Prettier would leave this file exactly as it is. ESLint, with the right rules turned on, reports two problems:
no-unused-vars:countis assigned and never used.eqeqeq: use===instead of==.
ESLint also parses the code into an AST. It then runs scope analysis, which records which variables are declared where and which identifiers refer to them. Each rule walks the tree, looks for its pattern, and reports a message with a location. A rule can also offer a fix, which is a small text edit such as replacing == with ===. ESLint only applies fixes when you ask it to.
How They Work Internally
The two tools use different parsers. ESLint uses Espree by default, and Prettier picks a parser based on the file type. The important difference is what each tool produces:
| Aspect | Formatter (Prettier) | Linter (ESLint) |
|---|---|---|
| Main output | A rewritten file | A list of problems |
| Changes behavior? | Never | Fixes can, so they are opt-in |
| Configuration | A handful of options | Hundreds of rules, each with a severity |
| Typical concerns | Indentation, quotes, line width, commas | Unused code, unsafe comparisons, undefined names, banned APIs |
| Needs human judgment? | No | Often |
| Languages | JS, TS, JSON, CSS, Markdown, HTML, YAML, and more | JavaScript by default; others through plugins |
Where They Overlap, and Why That Causes Trouble
For years, ESLint shipped core rules for formatting, such as indent, quotes, semi, and comma-dangle. If you enable those rules and also run Prettier, the two tools can disagree. ESLint complains about code that Prettier just printed, or eslint --fix and Prettier keep rewriting the same lines back and forth on every save.
ESLint deprecated its formatting rules in v8.53.0, and the community package @stylistic/eslint-plugin now maintains them. Many shared configs and older projects still turn on stylistic rules, though, so the overlap still causes problems in real projects. A later chapter shows how eslint-config-prettier switches those rules off so each tool keeps to its own job.
Common Mistakes
- Expecting Prettier to catch bugs. Prettier will tidy up a function that reads an undefined variable, and the bug stays.
- Running ESLint stylistic rules alongside Prettier. This causes the conflicts described above. Pick one tool to own formatting, and in this tutorial that tool is Prettier.
- Treating a clean lint run as proof of correctness. Linters catch known patterns. They do not replace tests or type checking.
- Letting warnings pile up. When a project has hundreds of ignored warnings, people stop noticing new ones. A rule should either matter enough to fix or be turned off.
- Applying lint fixes blindly. Changing
==to===in thefindUserexample changes the result from a user object tonull. Review fixes that change behavior.
How This Tutorial Divides the Work
The rest of the tutorial follows the split from this chapter. You will set up ESLint and choose rules for code quality. Next you will set up Prettier for formatting and make sure the two tools do not overlap. Then you will run both automatically in your editor, in a pre-commit hook, and in CI. Keep the two questions in mind as you go: "how should it look?" belongs to Prettier, and "is it likely wrong?" belongs to ESLint.
Exercise
A teammate proposes enabling ESLint's quotes rule to enforce single quotes, while the project already runs Prettier with singleQuote: true. What problem might this cause, and what would you recommend?
Show answer
Both tools would now own the same formatting concern. If their settings ever disagree, for example over how strings that contain quotes are handled, ESLint reports errors on code Prettier just printed, and the two tools' fixes can keep undoing each other. Quote style can be satisfied by changing only punctuation, so it belongs to the formatter. Leave it to Prettier and turn off ESLint's formatting rules, which is what eslint-config-prettier does.
Exercise
For each item, decide whether it is a linter concern or a formatter concern: (1) a variable declared but never read, (2) lines longer than 100 characters, (3) using == instead of ===, (4) missing trailing commas in multi-line arrays, (5) calling alert() in production code.
Show answer
(1) Linter: removing it changes which code exists. (2) Formatter: line breaks fix it. (3) Linter: the change can alter behavior. (4) Formatter: punctuation only. (5) Linter: it is a team policy about which APIs are allowed, and fixing it means changing the code's behavior.
Exercise
Run the findUser example in Node.js, then change == to === and run it again. What changes in the output, and what does this show about automatic lint fixes?
Show answer
The first run prints the user object { id: 1, name: 'Ada' }. After the change it prints null, because the string "1" is not strictly equal to the number 1. A lint fix can change runtime behavior, so you should review fixes like this one, and the real fix may be to convert the ID to a number before comparing.