Configure Claude Code hooks for automated pre/post actions
✓Works with OpenClaudeYou are a Claude Code automation engineer. The user wants to configure Claude Code hooks for automated pre/post actions on file operations, code generation, and skill execution.
What to check first
- Run
claude-code config listto see current hook configuration - Check if
.claude-code/hooks.jsonexists in your project root - Verify Node.js runtime is available (
node --version)
Steps
- Create
.claude-code/hooks.jsonin your project root with the hook registry structure - Define pre-hooks that run before file writes using the
beforeWriteevent type - Define post-hooks that run after code generation using the
afterGenerateevent type - Specify the hook handler path (relative to project root) for each hook entry
- Add hook metadata including
name,description,priority(0-100, higher runs first), andasyncflag - Create handler modules that export a default function accepting
contextobject with file path, content, and operation type - Register hooks using
claude-code hook register <hook-name> <handler-path>command - Test hooks with a trial file write to verify execution order and error handling
Code
{
"version": "1.0",
"hooks": [
{
"id": "lint-before-write",
"event": "beforeWrite",
"name": "Lint Before Write",
"description": "Run ESLint on generated code before writing to disk",
"handler": ".claude-code/hooks/lint-handler.js",
"priority": 90,
"async": true,
"config": {
"ruleset": ".eslintrc.json",
"fix": true
},
"enabled": true
},
{
"id": "format-after-generate",
"event": "afterGenerate",
"name": "Format After Generate",
"description": "Auto-format generated code with Prettier",
"handler": ".claude-code/hooks/format-handler.js",
"priority": 85,
"async": true,
"config": {
"parser": "babel",
"semi": true,
"singleQuote": false
},
"enabled": true
},
{
"id": "git-track-changes",
"event": "afterWrite",
"name": "Git Track Changes",
"description": "Stage modified files to git automatically",
"handler": ".claude-code/hooks/git-handler.js",
"priority": 50,
"async": false,
"enabled": true
}
]
}
// .claude-code/hooks/lint-handler.js
const { execSync } = require('child_process');
const path = require('path');
module.exports = async function lintHandler(context) {
const { filePath, content, config } =
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
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 Claude Code Skills
Other Claude Code skills in the same category — free to download.
CLAUDE.md Writer
Write effective CLAUDE.md project configuration files for Claude Code
MCP Server Setup
Set up Model Context Protocol servers for Claude Code
Custom Slash Commands
Create custom slash commands for Claude Code workflows
Skills Writer
Write custom Claude Code skill files with proper format
Memory Setup
Configure Claude Code persistent memory system
Permissions Config
Configure Claude Code permission settings and tool access
Agent SDK Setup
Build custom agents with Claude Agent SDK
Context Management
Optimize context window usage in Claude Code conversations
Want a Claude Code 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.