lint-staged与prettier在precommit阶段无法运行的问题求助
I’ve run into this exact head-scratcher before—total frustration when the same setup works flawlessly in other repos! Let’s break down the most likely fixes step by step:
1. Double-check Husky hook installation
Husky v0.14.3 relies on setting up Git hooks during initial installation. Let’s confirm the hook exists:
- Navigate to your repo’s
.git/hooks/folder and look for a file namedpre-commit(no file extension). - If it’s missing, force Husky to rebuild the hooks:
npm rebuild husky - If that fails, delete your
node_modulesfolder andpackage-lock.json, then re-runnpm installto start fresh—sometimes corrupted installs break hook setup.
2. Test scripts directly (bypass the Git hook)
Let’s isolate whether the issue is with the hook itself or the underlying commands:
- Run your precommit script manually:
npm run precommit - If this works, the problem is the Git hook not triggering. If it fails, there’s an issue with your
lint-stagedconfig or dependency conflicts.
3. Check Git’s custom hook path setting
Git lets you override the default hooks directory with core.hooksPath. Run this to check:
git config core.hooksPath
If this returns a path other than .git/hooks/, Husky’s hooks won’t be used. Reset it to default with:
git config --unset core.hooksPath
4. Validate lint-staged and prettier independently
Your config looks correct, but let’s verify each component:
- Run
lint-stageddirectly to see if it executes:npx lint-staged - Test prettier on a sample file to confirm it works:
npx prettier --write your-test-file.js
If either command throws errors, you’ll know exactly where to focus (e.g., missing file extensions in your config, or a corrupted prettier install).
5. Try updating dependencies (optional)
Your versions are quite outdated (Husky v0.14 is years old, Prettier 1.12 dates back to 2018). While you said the same config works elsewhere, hidden dependency conflicts in this repo could be the culprit. If the above steps don’t fix it, try updating to newer compatible versions:
npm install husky@latest lint-staged@latest prettier@latest --save-dev
Note: Husky v4+ uses a new configuration format (you’ll need to add a husky field to package.json), so you’ll need to adjust your setup to match if you go this route.
内容的提问来源于stack exchange,提问作者Andrew Horn

