husky ".husky/pre-commit: command not found" - Fix Hook Script in CI
This is the runtime side of husky: a Git hook (e.g. .husky/pre-commit) runs but a command inside it - lint-staged, a linter, a formatter - is not found because it was not installed or is not on PATH in that context.
What this error means
A commit or a hook-triggering step fails with .husky/pre-commit: line N: <tool>: command not found. The hook itself runs, but the tool it calls is missing - often in CI commits or environments without devDependencies.
.husky/pre-commit: line 4: lint-staged: command not found
husky - pre-commit script failed (code 127)Common causes
The hook’s tool is not installed in this context
A hook invoking lint-staged/eslint/prettier fails when those devDependencies are absent (e.g. a production install) or the bare binary is expected on PATH.
PATH does not include node_modules/.bin
Hooks run in a minimal shell. Calling a bare tool name relies on it being on PATH; without npx/node_modules/.bin, it is not found.
How to fix it
Invoke hook tools via the local binary
Call the tool through npm/npx so it resolves from node_modules.
# .husky/pre-commit
npx --no-install lint-staged
# ensure the tool is a devDependency:
npm install -D lint-stagedSkip hooks where they do not belong
- Do not rely on devDependency hook tools in production installs.
- In CI that does not need hooks, set
HUSKY=0to disable them. - Ensure any committing CI job installs the hook tools first.
How to prevent it
- Call hook tools via
npx --no-installornode_modules/.bin. - Keep hook tools as installed devDependencies.
- Disable husky (
HUSKY=0) in contexts without dev deps.