Skip to main content
hook

build-on-change — Claude Code hook: what it does, how to install

Runs your build pipeline automatically whenever Claude Code edits a file. Detects build setups for Node.js, Make, Rust, and Java projects.

Install into Claude Code · claude-code-templates All methods and platforms ↓
npx claude-code-templates@latest --hook automation/build-on-change
Open on GitHub

What build-on-change does

The hook attaches to the PostToolUse event with an Edit matcher. As soon as Claude Code finishes modifying any file, it executes a shell script inside the workspace.

The script inspects root files to pick the right command. It looks into package.json for a build script to invoke npm run build or yarn build. If not found, it checks for Makefile (make), Cargo.toml (cargo build), pom.xml (mvn compile), or build.gradle (./gradlew build).

Standard error is suppressed via 2>/dev/null combined with || true. Because of this suppression, a broken compilation does not stop the agent or print failure logs back to the context.

What it helps with

  • rebuild projects automatically after agent file changes
  • compile Rust code via cargo on source edits
  • trigger frontend bundle builds after script updates
  • run make or gradle targets without manual console input

Example prompts for the agent

  • Update the interface definitions in src/index.ts
  • Add input validation to the parser in src/main.rs

How to install build-on-change

1claude-code-templates

npx claude-code-templates@latest --hook automation/build-on-change

2Manually: put the files in place

.claude/settings.json

The hooks block: event (PreToolUse, PostToolUse, Stop…) → command. Personal ones go to ~/.claude/settings.json

Step by step, with commands for each platform: Claude Code hooks: what they are, where to configure them, how to block a risky command

Questions about build-on-change

How to install build-on-change in Claude Code?

Run npx claude-code-templates@latest --hook automation/build-on-change or place the hook declaration inside .claude/settings.json.

Which build systems does build-on-change support?

It recognizes projects managed with package.json (npm or yarn), Makefile, Cargo.toml, pom.xml, and build.gradle.

How does the hook handle compilation failures?

The script discards errors using /dev/null and appends || true, ensuring broken builds do not block the active agent session.

Source text

build-on-change.json · MIT
{
  "description": "Automatically trigger build processes when source files change. Detects common build tools and runs appropriate build commands.",
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit",
        "hooks": [
          {
            "type": "command",
            "command": "if [[ -f package.json ]] && grep -q '\"build\"' package.json; then npm run build 2>/dev/null || yarn build 2>/dev/null || true; elif [[ -f Makefile ]]; then make 2>/dev/null || true; elif [[ -f Cargo.toml ]]; then cargo build 2>/dev/null || true; elif [[ -f pom.xml ]]; then mvn compile 2>/dev/null || true; elif [[ -f build.gradle ]]; then ./gradlew build 2>/dev/null || true; fi"
          }
        ]
      }
    ]
  }
}

© davila7 · MIT · davila7/claude-code-templates

Similar hooks

All hooks for Automation and no-code: n8n, Zapier, Make →

From the same set804

The whole set →

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor