Sub topic 13
Adding Lint and Format Scripts
This chapter assumes ESLint and Prettier are already installed and configured in your project, with eslint-config-prettier keeping them from conflicting. You should also know how to edit package.json and run commands with npm.
Why Scripts Instead of Raw Commands
You can run npx eslint . and npx prettier . --write by hand. That works, but every developer then has to remember the flags, and every tool, such as a Git hook or a CI job, needs its own copy of the command. An npm script gives the command a name and stores it in package.json. Everyone then runs the same command with npm run lint, and if the flags change, you edit them in one place.
A Standard Set of Scripts
Most projects define four scripts: two that only check and two that change files.
{
"scripts": {
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"format": "prettier . --write",
"format:check": "prettier . --check"
}
}| Script | Modifies files | Typical use |
|---|---|---|
| `lint` | No | Report problems locally or in CI |
| `lint:fix` | Yes | Apply ESLint autofixes before committing |
| `format` | Yes | Rewrite files in Prettier style |
| `format:check` | No | Fail if any file is unformatted |
Scripts inside package.json can call eslint and prettier directly without npx, because npm adds node_modules/.bin to the PATH while a script runs.
Exit Codes Matter
Automation reads a command's exit code, not its output. A non-zero code means failure.
eslint .exits with 1 if any rule set to"error"fails. Warnings alone still give exit code 0.prettier . --checkexits with 1 if any file would be reformatted.
If you want warnings to fail the build too, set a warning limit:
{
"scripts": {
"lint": "eslint . --max-warnings 0"
}
}Common Mistakes
- Running the fix scripts in CI.
formatandlint:fixrewrite files and then succeed, so the job passes without proving anything. CI should runlintandformat:check. - Forgetting the
--separator. If you want to pass extra flags through npm, put--before them:npm run lint -- --fix. Without it, npm reads the flag as its own. - Formatting generated output.
prettier .processes every file it supports. Adddist/, coverage reports, and similar folders to.prettierignore. Otherwiseformat:checkfails on files nobody edits. Prettier skipsnode_modulesby default. - Using different commands in different places. If a hook or a CI job calls
eslint srcwhile the script callseslint ., they will report different results. Point every tool at the npm script.
The next chapters reuse these scripts. They run them on staged files before each commit and enforce them in CI.
Exercise
Your CI job runs npm run lint, and ESLint reports three no-unused-vars warnings, but the job still passes. Why, and how would you make it fail?
Show answer
ESLint exits with code 0 when it finds only warnings, so CI treats the run as a success. Change the script to eslint . --max-warnings 0, or set the rule's severity to "error".
Exercise
A teammate proposes running npm run format in CI "to make sure code is formatted." What is wrong with this?
Show answer
prettier --write rewrites unformatted files and exits successfully, so the job passes even when committed code is unformatted. CI should run npm run format:check, which exits with 1 when any file needs formatting.