Sub topic 13
Adding Lint and Format Scripts
You can already run ESLint and Prettier by hand with npx. This chapter puts those commands into package.json scripts, so everyone on the team and every automated system runs them the same way. This chapter assumes you can edit package.json, run commands with npm, and have the ESLint flat config and Prettier setup from earlier chapters in place.
Why scripts instead of raw commands
A command like npx eslint . --fix is easy to type, but it hides choices: which folder to scan, which flags to pass, and whether warnings count as failures. If those choices live only in one person's head, the editor, the pre-commit hook, and CI drift apart. An npm script is a named shell command stored in the scripts field of package.json. When you run it, npm adds node_modules/.bin to the PATH, so it finds the project's own ESLint and Prettier and never a different version installed globally.
Scripts give you:
- One definition of "lint" and "format" for every developer and tool.
- The locally installed tool version, pinned by your lockfile.
- Short, memorable commands (
npm run lint) that later chapters can call from hooks and CI.
The four core scripts
Most projects need two pairs of scripts. One pair checks and never changes files. The other pair fixes and writes changes to disk.
{
"scripts": {
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"format": "prettier . --write",
"format:check": "prettier . --check"
}
}| Script | Modifies files | Typical use | Fails when |
|---|---|---|---|
| `lint` | No | CI, reviewing code | Any rule at `"error"` is violated |
| `lint:fix` | Yes | Local cleanup | Errors remain that autofix cannot fix |
| `format` | Yes | Local cleanup | Prettier cannot parse a file |
| `format:check` | No | CI, pre-push checks | Any file differs from Prettier's output |
The colon in lint:fix is just a naming convention. npm treats it as part of the name. Grouping related scripts this way keeps the list readable as it grows.
What `.` actually covers
With flat config, eslint . lints files that match the files patterns in your config, plus **/*.js, **/*.mjs, and **/*.cjs by default. It skips node_modules and .git automatically, along with any ignores patterns you set in eslint.config.js. ESLint does not read .gitignore on its own.
prettier . formats every file type Prettier supports: JavaScript, JSON, Markdown, CSS, YAML, and more. Prettier 3 skips files listed in .gitignore and .prettierignore. Because the two tools decide what to include in different ways, a build folder that Prettier skips may still be linted by ESLint unless you add it to ESLint's ignores.
Exit codes are the contract
Hooks and CI decide pass or fail from the process exit code, not from what the tool prints. Knowing these codes explains most "why did CI pass?" surprises.
| Tool | Exit 0 | Exit 1 | Exit 2 |
|---|---|---|---|
| ESLint | No errors (warnings allowed) | At least one error, or too many warnings | Config or internal failure |
| Prettier `--check` | All files formatted | Some files need formatting | Invalid config or parse failure |
The detail that catches people most often is that ESLint warnings do not fail the run. A rule set to "warn" prints output and still exits with 0. If you want warnings to block merges, set a ceiling:
{
"scripts": {
"lint": "eslint . --max-warnings 0"
}
}With --max-warnings 0, a single warning makes ESLint exit with 1. Some teams leave warnings non-blocking during a gradual cleanup and add the flag once the warning count reaches zero.
Combining scripts
Running two commands every time is tedious. Add composite scripts that chain them:
{
"scripts": {
"lint": "eslint . --max-warnings 0",
"lint:fix": "eslint . --fix",
"format": "prettier . --write",
"format:check": "prettier . --check",
"check": "npm run lint && npm run format:check",
"fix": "npm run lint:fix && npm run format"
}
}&& runs the second command only if the first exits with 0. That works in POSIX shells and in Windows cmd.exe, which is the shell npm uses on Windows. Avoid ; for this purpose. It behaves differently across shells, and in a check script it would let a lint failure slip through when the format check passes afterward.
Order matters for fix scripts
Run eslint --fix before prettier --write. Some ESLint autofixes insert or move code without regard for line length or spacing, and running Prettier last gives the final layout. If you configured eslint-config-prettier as described earlier, the two tools no longer fight over formatting rules, so a second pass is never needed.
Passing extra arguments
You can forward flags to the underlying command by putting them after --:
npm run lint -- --format json
npm run lint:fix -- src/utils
npm run format:check -- "src/**/*.js"Everything after -- is appended to the script's command. The second example becomes eslint . --fix src/utils. ESLint lints both . and src/utils, and since . already includes src/utils, the extra argument does not narrow the run. To lint only part of the project, call the tool directly with npx eslint src/utils, or write a script that takes no default path.
Speeding up large projects with caches
Both tools can skip files that have not changed since the last run:
{
"scripts": {
"lint": "eslint . --cache --max-warnings 0",
"format:check": "prettier . --check --cache"
}
}ESLint writes its cache to .eslintcache in the project root. Prettier stores its cache in node_modules/.cache/prettier. Add .eslintcache to .gitignore so it never gets committed. Edge cases to know about:
- The ESLint cache becomes stale when a plugin version changes. ESLint invalidates entries when the config changes, but if results look wrong after an upgrade, delete
.eslintcache. - Fresh CI checkouts have no cache, so
--cachedoes nothing there unless you persist the file between runs. --cache-locationlets you move the ESLint cache, for example intonode_modules/.cache/eslint/so it lives with other build artifacts.
Common mistakes
- Quoting globs. Write
prettier "src/**/*.js" --checkwith quotes, escaped as\"insidepackage.json. Without quotes, your shell may expand the glob itself, and it often expands**differently from Prettier. - Checking the wrong scope.
prettier src --checkignores your root Markdown and JSON files. Use.and rely on ignore files to exclude what you do not want. - Hiding failures with
|| true. Teams sometimes append this to quiet a noisy script. It forces exit code 0 and turns the check into a no-op. - Using global installs. If a script works only because ESLint is installed globally, a teammate without it gets "command not found". Keep both tools in
devDependencies.
Exercise
Your lint script is "eslint .". CI shows 14 ESLint warnings, and the job is green. Your team wants any warning to fail the build. What do you change, and why did the job pass before?
Show answer
Change the script to "eslint . --max-warnings 0". ESLint exits with code 0 when it finds only warnings, and CI judges success by the exit code. With --max-warnings 0, any warning raises the exit code to 1, so the job fails.
Exercise
A teammate writes "check": "npm run format:check; npm run lint". Lint fails but the check sometimes reports success. Explain and fix it.
Show answer
With ;, the shell runs both commands regardless of results, and the script's exit code depends on how the shell treats the chain. That behavior differs across shells, and it does not reliably report a failure from either command. Use "check": "npm run lint && npm run format:check" so the first non-zero exit stops the chain and fails the script.
Exercise
Why should fix run eslint --fix before prettier --write, and what should CI run instead?
Show answer
ESLint autofixes can change code layout without respecting Prettier's style, so running Prettier last produces the final formatting. CI should run only the check scripts (lint and format:check). Rewriting files in CI hides problems instead of reporting them.