Skip to main content

Husky Setup

1

Install Husky

2

Add pre-commit hook

Create .husky/pre-commit:
.husky/pre-commit
Make it executable:
3

Test the hook

Make a change and commit:
React Doctor will run automatically before the commit completes.

Pre-Push Hook

For longer-running scans, use a pre-push hook instead:
1

Create pre-push hook

Create .husky/pre-push:
.husky/pre-push
Make it executable:
2

Test the hook

React Doctor will run before the push completes.

Lint-Staged Integration

Run React Doctor only on staged files for faster commits:
1

Install lint-staged

2

Configure lint-staged

Add to package.json:
package.json
Or create .lintstagedrc.json:
.lintstagedrc.json
3

Update pre-commit hook

Modify .husky/pre-commit:
.husky/pre-commit

Monorepo Hooks

Detect Changed Project

Run React Doctor only on the affected workspace project:
.husky/pre-commit

Scan All Projects

.husky/pre-push

Conditional Hooks

Skip on WIP Commits

Allow WIP commits to skip the hook:
.husky/pre-commit

Skip with Environment Variable

Bypass the hook when needed:
.husky/pre-commit
Usage:

Performance Optimization

Cache npx Downloads

Speed up hook execution by caching React Doctor:
.husky/pre-commit

Skip Linting for Speed

Run only dead code detection:
.husky/pre-commit

Quick Score Check

Just verify the score hasn’t dropped:
.husky/pre-commit

Git Bypass

Users can always bypass hooks if needed:
Git hooks run locally and can be bypassed. For enforced checks, use CI/CD pipelines.

Complete Workflow Example

Here’s a complete setup with both pre-commit and pre-push hooks:
1

Install dependencies

2

Configure lint-staged

package.json
3

Create pre-commit hook

.husky/pre-commit
4

Create pre-push hook

.husky/pre-push
5

Test the setup

Troubleshooting

Hook Not Running

Ensure the hook is executable:

npx Takes Too Long

Cache React Doctor globally:
Then use in hooks:

Hook Fails in CI

Git hooks only run locally. For CI, use CI/CD Integration.