Configure pre-commit hooks (Husky, lint-staged)
✓Works with OpenClaudeYou are a DevOps/workflow engineer. The user wants to set up pre-commit hooks using Husky and lint-staged to automatically lint and format code before commits.
What to check first
- Run
git initor confirm you're in a git repository (git status) - Check Node.js and npm are installed:
node --version && npm --version - Verify no existing
.huskydirectory (indicates Husky already installed)
Steps
- Install Husky as a dev dependency:
npm install husky --save-dev - Initialize Husky in your project:
npx husky install - Install lint-staged:
npm install lint-staged --save-dev - Create the pre-commit hook:
npx husky add .husky/pre-commit "npx lint-staged" - Configure lint-staged in
package.jsonunder the"lint-staged"key with file patterns and commands - Add ESLint or Prettier commands to target specific file types (
.js,.jsx,.ts,.tsx) - Commit the
.huskydirectory to git (it contains executable shell scripts) - Test by making a change, staging it, and running
git commit
Code
{
"name": "my-project",
"version": "1.0.0",
"scripts": {
"prepare": "husky install",
"lint": "eslint . --ext .js,.jsx,.ts,.tsx",
"format": "prettier --write ."
},
"devDependencies": {
"eslint": "^8.0.0",
"prettier": "^3.0.0",
"husky": "^8.0.0",
"lint-staged": "^15.0.0"
},
"lint-staged": {
"*.{js,jsx,ts,tsx}": [
"eslint --fix",
"prettier --write"
],
"*.{json,md}": "prettier --write"
}
}
#!/bin/bash
# .husky/pre-commit (automatically created, shown for reference)
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
npx lint-staged
// Alternative: .lintstagedrc.js for complex config
module.exports = {
"*.{js,jsx,ts,tsx}": (files) => {
return [
`eslint --fix ${files.join(" ")}`,
`prettier --write ${files.join(" ")}`
];
},
"*.json": "prettier --write",
"README.md": "prettier --write"
};
Pitfalls
- Husky's
.huskydirectory must be committed to git — the hook scripts won't run without them in version control - `npx hu
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Workflow Automation Skills
Other Claude Code skills in the same category — free to download.
Git Workflow
Set up Git branching workflow (GitFlow, trunk-based)
Auto Formatter
Set up auto-formatting (Prettier, ESLint)
Issue Template
Create GitHub issue and PR templates
Dependabot Setup
Configure Dependabot/Renovate for auto-updates
Release Workflow
Automate release workflow with changelogs
Code Owner Setup
Configure CODEOWNERS file
Branch Protection
Set up branch protection rules
Auto Labeler
Auto-label PRs based on changed files
Want a Workflow Automation skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.