You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

lint-staged与prettier在precommit阶段无法运行的问题求助

Troubleshooting lint-staged & prettier not running on pre-commit hook

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 named pre-commit (no file extension).
  • If it’s missing, force Husky to rebuild the hooks:
    npm rebuild husky
    
  • If that fails, delete your node_modules folder and package-lock.json, then re-run npm install to 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-staged config 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-staged directly 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:50:24