# ESLint and Prettier for a Node Project

ESLint reads a Node project's code and reports likely mistakes, such as a variable nobody uses or a == that hides text from a URL, and Prettier lays out every file the same way, so you set both up once with a flat config, globals.node and npm scripts.

Journeyman · Node.js · Software Development · Updated November 19, 2026

By Gregor Dzierzon

## After this lesson you can

- Set up ESLint for a Node project with a flat config, globals.node and npm scripts, and fix the code it reports instead of switching rules off
- Format a project with Prettier from npm scripts and on save, and tell what Prettier changes from what ESLint reports

## Key takeaways

- A linter such as ESLint reports likely mistakes without running the code. A formatter such as Prettier changes only how the code looks, never what it does.
- In a Node project, eslint.config.js lists js.configs.recommended and sets globals.node, so ESLint knows that process and console are real.
- Fix the code ESLint points at, not the rule. A route parameter is text, so === needs Number(id) where == only hid the difference.
- Express needs all four parameters of an error handler, so name the unused one _next and tell no-unused-vars to skip names that start with _.
- The scripts lint, format and format:check run the same checks on every computer, and Format On Save in VS Code formats each file as you save it.

On a team, every change is read by someone before it's merged, and the first things a reviewer spots are the small ones: a variable nobody uses, a `==` that should be `===`, single quotes in one file and double quotes in the next. Most teams let two tools find those for them, ESLint and Prettier, and won't merge code until both say it's clean. You'll set them up on a Node API in this lesson, the way you would on your first day on a team.

This chapter's example is the Lost and Found Desk, a campus API that lists the things students find (a water bottle in the library, a textbook in Science Hall) and records who claims them. Three students work on it together, and each writes code a little differently.

## A spell-checker and a format button

A word processor helps you two ways. Its spell-checker underlines words that look wrong; it points, and you decide. Its format button lays out every page the same way, so nobody argues about margins.

Code has the same two helpers:

- A **linter** is the spell-checker. It reads your code without running it and reports likely mistakes. **ESLint** (said "E-S-lint") is the linter for JavaScript, in the browser and in Node.
- A **code formatter** is the format button. It rewrites how code is laid out (quotes, semicolons, spaces, line breaks) and never what it does. **Prettier** is the one most JavaScript projects use.

## Set up ESLint for Node

In the API's folder, install four packages as devDependencies (tools for working on the project, not for running it), at exact versions so the whole team gets the same rules:

```bash
npm install --save-dev --save-exact eslint@10.15.0 @eslint/js@10.0.1 globals@17.14.0 prettier@3.10.1
```

```text
added 79 packages, and audited 145 packages in 2s

50 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
```

`@eslint/js` (read "at eslint slash js") holds ESLint's recommended rules, and `globals` lists the names each place already provides. ESLint reads its settings from `eslint.config.js` at the top of the project:

```javascript
import js from "@eslint/js";
import globals from "globals";
import { defineConfig } from "eslint/config";

export default defineConfig([
  js.configs.recommended,
  {
    languageOptions: {
      globals: globals.node,
    },
    rules: {
      eqeqeq: "error",
      "prefer-const": "error",
    },
  },
]);
```

This is a **flat config**: one list of settings, applied in order. `js.configs.recommended` turns on the rules the ESLint team recommends, such as **no-unused-vars** and **no-undef** (a name that was never declared). `rules` adds two more: **eqeqeq** asks for `===` (read "triple equals") instead of `==`, and **prefer-const** asks for `const` when a `let` never changes.

`globals.node` matters most in a Node project. Leave it out, and ESLint doesn't know the names Node gives every file, so `server.js` gets:

```text
  3:14  error  'process' is not defined  no-undef
  6:3   error  'console' is not defined  no-undef
```

## Read the report, then fix the code

`npx eslint .` lints the whole project (the `.`, read "dot", is this folder):

```bash
npx eslint .
```

```text
C:\Users\Sam\...\lost-and-found-api\app.js
  23:25  error  'next' is defined but never used  no-unused-vars

C:\Users\Sam\...\lost-and-found-api\items.js
  8:7   error  'item' is never reassigned. Use 'const' instead  prefer-const
  8:43  error  Expected '===' and instead saw '=='              eqeqeq
  9:9   error  'searchedAt' is assigned a value but never used  no-unused-vars

✖ 4 problems (4 errors, 0 warnings)
  1 error and 0 warnings potentially fixable with the `--fix` option.
```

Each line gives the line and column (`8:43` is line 8, character 43), what's wrong and the rule's name. Here is `findItem` in `items.js`, which the route `GET /items/:id` calls with `req.params.id`:

```javascript
export function findItem(id) {
  let item = items.find((each) => each.id == id);
  const searchedAt = new Date();
  return item;
}
```

`searchedAt` goes (nothing uses it), and `let` becomes `const`, or the variable goes too. The `==` needs thought. A route parameter is always text, as *Route and Query Parameters* showed, so `id` is `"2"`, not `2`. `==` changes types before it compares, which hid that:

```javascript
console.log("2" == 2);
console.log("2" === 2);
console.log(Number("2") === 2);
console.log("02" == 2);
```

```text
true
false
true
true
```

Swap in `===` alone and every item answers 404. Turn the text into a number first:

```javascript
export function findItem(id) {
  return items.find((each) => each.id === Number(id));
}
```

Fix the code itself, never the rule: switching eqeqeq off would leave `"02"` finding item 2.

## The parameter Express needs

The last problem is in the error handler, `app.use((err, req, res, next) => { ... })`. It never calls `next`, but it can't drop it: Express recognizes an error handler by its four parameters. The usual fix is a naming habit. Rename it `_next`, and tell no-unused-vars to skip parameters whose names start with `_` (read "underscore"):

```javascript
      "no-unused-vars": ["error", { argsIgnorePattern: "^_" }],
```

That line goes in `rules`. Run `npx eslint .` again and it prints nothing: no news is good news.

## Prettier, scripts and format on save

ESLint has no opinion on layout. Luis's route is crooked, single-quoted and missing its semicolons:

```javascript
app.get('/items', (req,res)=>{
    res.json(items)
})
```

`npx prettier --check .` lists the files Prettier would change, and changes nothing:

```text
Checking formatting...
[warn] app.js
[warn] Code style issues found in the above file. Run Prettier with --write to fix.
```

`npx prettier --write .` formats them, naming each file it read and marking the ones it left alone. It skips what `.gitignore` lists, such as `node_modules`:

```text
app.js 56ms
eslint.config.js 6ms (unchanged)
items.js 5ms (unchanged)
package-lock.json 30ms (unchanged)
package.json 1ms (unchanged)
server.js 3ms (unchanged)
```

The route now reads `app.get("/items", (req, res) => {`, with double quotes, spaces and semicolons. Teams stop arguing about style: the tool decides, the same way for everyone.

Nobody types these commands from memory. Add three npm scripts to `package.json`:

```json
  "scripts": {
    "start": "node server.js",
    "dev": "node --watch server.js",
    "test": "node --test",
    "lint": "eslint .",
    "format": "prettier --write .",
    "format:check": "prettier --check ."
  },
```

`npm run format:check` changes nothing, so it's the one an automatic check runs; the next lesson, *Tests on Every Push with GitHub Actions*, runs it with `npm run lint` on every push.

Better still, let VS Code run the tools as you type. Install the ESLint and Prettier extensions (`code --install-extension dbaeumer.vscode-eslint` and `code --install-extension esbenp.prettier-vscode`), so ESLint's problems show as squiggly underlines. Then in **File > Preferences > Settings** (**Code > Settings** on a Mac), tick **Editor: Format On Save** and set **Editor: Default Formatter** to **Prettier - Code formatter**. Every save now formats the file.

## Do it: clean up what ESLint found

Every found item goes on a numbered shelf once it's put away; shelf 0 is the one right behind the desk. Until then, its shelf box is empty (`""`). `itemsToShelve` counts the items still waiting for a shelf. Mei says the desk shows 3 items to shelve when it should say 2: one went onto shelf 0. Before anyone looks for the bug, `npx eslint .` printed this for her function:

```text
  2:7   error  'waiting' is never reassigned. Use 'const' instead  prefer-const
  2:49  error  Expected '===' and instead saw '=='                 eqeqeq
  3:9   error  'total' is assigned a value but never used          no-unused-vars

✖ 3 problems (3 errors, 0 warnings)
  1 error and 0 warnings potentially fixable with the `--fix` option.
```

**Your job:** fix all three in `itemsToShelve(shelves)`, without switching a rule off.

- `shelves`: one entry for each found item, its shelf number (such as `4` or `0`) or `""` while it waits for a shelf.
- It returns how many entries are `""`.
- The code has no `==` or `!=`, no `let`, and no `total`.

Worked through: `itemsToShelve([4, 0, "", ""])` has two empty boxes, so it returns `2`. Shelf `0` is a shelf, not an empty box.

Practiced live on the lesson page: write the JavaScript right there and check it.

## Check yourself

2 questions to try on the lesson page.

## So what

ESLint catches the mistakes that slip past you, Prettier makes every file look like one careful person wrote it, and with both in npm scripts, the whole team runs the same checks, which is exactly what a CI pipeline needs next.

## Key terms

- [Linter](https://www.ask-codey.com/glossary/linter)
- [ESLint](https://www.ask-codey.com/glossary/eslint)
- [Prettier](https://www.ask-codey.com/glossary/prettier)
- [Flat Config](https://www.ask-codey.com/glossary/flat-config)
- [npm scripts](https://www.ask-codey.com/glossary/npm-scripts)

## Related

- [Tests on Every Push with GitHub Actions](https://www.ask-codey.com/lessons/node-pro-github-actions-ci)
- [Package Versions, npm ci and npm audit](https://www.ask-codey.com/lessons/node-npm-versions-and-lock-file)
- [Exercise: Lost and Found CI](https://www.ask-codey.com/lessons/exercise-node-lost-and-found-ci)

Source: https://www.ask-codey.com/lessons/node-pro-eslint-and-prettier
