Sub topic 14
Checking Staged Files with Husky and lint-staged
This chapter assumes you already have ESLint and Prettier configured, along with the lint and format npm scripts from the previous chapter. It also assumes you know how git add stages files.
Running npm run lint by hand is easy to forget. A pre-commit hook is a script that Git runs right before it creates a commit. If the script exits with an error, Git cancels the commit. Two tools make this practical:
- Husky installs Git hooks from files committed to your repository, so every contributor gets the same hooks after running
npm install. - lint-staged runs commands only on the files you staged. It does not touch the whole project, which keeps the hook fast.
Installing the tools
npm install --save-dev husky lint-staged
npx husky init
echo "npx lint-staged" > .husky/pre-commitnpx husky init creates a .husky/ folder with a sample pre-commit file. It also adds "prepare": "husky" to your package.json scripts. npm runs prepare automatically after npm install, which is how teammates get the hooks. The echo line replaces the sample command with npx lint-staged.
Configuring lint-staged
lint-staged reads a lint-staged key in package.json. Each key is a glob, and each value is one command or a list of commands:
{
"scripts": {
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"format": "prettier . --write",
"format:check": "prettier . --check"
},
"lint-staged": {
"*.{js,mjs,cjs}": ["eslint --fix", "prettier --write"],
"*.{json,md,css}": "prettier --write"
}
}lint-staged adds the matching staged file paths to the end of each command. eslint --fix becomes something like eslint --fix src/app.js src/util.js. Commands in a list run in order, so ESLint fixes code before Prettier formats it. Any changes the commands make are added back to the commit automatically.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| Hook never runs | `prepare` did not run, or the repo was cloned before Husky was added | Run `npm install` again |
| Hook runs `npm test` | Sample `pre-commit` file was not replaced | Overwrite it with `npx lint-staged` |
| Whole project is linted | Commands say `eslint .` | Remove the `.`, because lint-staged passes the file paths |
| "No staged files match" | Glob does not cover your extensions | Add the extension to the glob |
Exercise
You change a line in a.js and stage it. You also change b.js but don't stage it. b.js contains an unused variable that the no-unused-vars rule reports as an error. Does the commit succeed, and why?
Show answer
Yes. lint-staged runs ESLint only on staged files, so b.js is never checked. Only a.js is linted, and it has no errors.
Exercise
Why should the lint-staged entry for JavaScript files say "eslint --fix" rather than "npm run lint:fix"?
Show answer
The lint:fix script runs "eslint . --fix" on the whole project. lint-staged would add the staged paths to the end of that command, but the "." would still make ESLint lint everything. The plain "eslint --fix" command receives only the staged file paths.