husky install command deprecation error in Prettier Git Hook setup
Problem Statement
When setting up Prettier's Git hooks on Windows using pnpm, developers encounter an error when running the recommended command from Prettier's official documentation:
pnpm exec husky install
Instead of initializing the Git hooks configuration, this returns the error:"install command is deprecated"
This deprecation error occurs because:
- You're using a newer version of husky (v9.0.1 or later)
- Prettier's official documentation might not yet reflect this change in husky's API
- The
install
command pattern has been replaced with a more straightforward method
Solution
Updated Command for husky v9+
As of husky v9.0.1, the install
command has been removed. The new equivalent is simply:
pnpm exec husky
This will create the .husky/
directory for your Git hooks without deprecation warnings.
Complete Workflow for Prettier with husky v9
- Install required packages:
pnpm add --save-dev husky prettier
- Initialize husky:
pnpm exec husky
- Add Prettier hook:
pnpm exec husky add .husky/pre-commit "pnpm exec prettier --write ."
- Make hook executable (Unix systems):
chmod +x .husky/pre-commit
Windows Note
Windows users typically don't need chmod
, but should verify Git Bash preserves execute permissions in the .husky/
directory.
Explanation
Why the Change?
Husky simplified its API in v9:
Version | Command | Equivalent in v9+ |
---|---|---|
< v9.0 | husky install | husky |
< v9.0 | husky install .myhooks | husky .myhooks |
The changelog explicitly states:
"Removed
husky install
. Usehusky
orhusky some/dir
for the same functionality"
Validating Your Setup
Verify your configuration by checking:
.husky/pre-commit
exists with contents:
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
pnpm exec prettier --write .
package.json
contains:
{
"devDependencies": {
"husky": "^9.0.0",
"prettier": "^3.0.0"
}
}
Peer Dependency Conflicts
If migrating from older projects, remove husky
dependencies older than v9 and delete any legacy configuration files like .huskyrc
.
Alternative Environments
For npm users:
npx husky
npx husky add .husky/pre-commit "npx prettier --write ."
For Yarn users:
yarn husky
yarn husky add .husky/pre-commit "yarn prettier --write ."
Conclusion
The husky install
deprecation reflects a simplification in husky's API. By either replacing:
pnpm exec husky install
→pnpm exec husky
npx husky install
→npx husky
yarn husky install
→yarn husky
You'll avoid the deprecation warning while setting up Prettier Git hooks. Always reference the official husky documentation for the latest best practices when configuring Git hooks in your JavaScript projects.