Skip to content

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:

bash
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:

bash
pnpm exec husky

This will create the .husky/ directory for your Git hooks without deprecation warnings.

Complete Workflow for Prettier with husky v9

  1. Install required packages:
bash
pnpm add --save-dev husky prettier
  1. Initialize husky:
bash
pnpm exec husky
  1. Add Prettier hook:
bash
pnpm exec husky add .husky/pre-commit "pnpm exec prettier --write ."
  1. Make hook executable (Unix systems):
bash
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:

VersionCommandEquivalent in v9+
< v9.0husky installhusky
< v9.0husky install .myhookshusky .myhooks

The changelog explicitly states:

"Removed husky install. Use husky or husky some/dir for the same functionality"

Validating Your Setup

Verify your configuration by checking:

  1. .husky/pre-commit exists with contents:
bash
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

pnpm exec prettier --write .
  1. package.json contains:
json
{
  "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:

bash
npx husky
npx husky add .husky/pre-commit "npx prettier --write ."

For Yarn users:

bash
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 installpnpm exec husky
  • npx husky installnpx husky
  • yarn husky installyarn 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.