Agent View / `/bg` worktrees don't inherit node_modules — hooks fail with MODULE_NOT_FOUND

Resolved 💬 3 comments Opened May 15, 2026 by DanceNinjas Closed May 18, 2026

Summary

When a background session is created via /bg (or spawned from the new claude agents UI), Claude Code automatically creates an isolated git worktree at .claude/worktrees/<task-name>/. Git worktrees share .git/ with the main repo but have their own working tree, and node_modules/ is typically .gitignored. As a result, the worktree has no node_modules/ directory, and any hook in .claude/hooks/ that loads a Node package fails immediately with node:internal/modules/cjs/loader:1459 (MODULE_NOT_FOUND).

The failure cascades: every tool call in the bg session triggers hooks, and they all fail, leaving the session unable to do useful work.

Environment

  • Claude Code: 2.1.142
  • macOS: Darwin 25.2.0
  • Project layout: hooks at .claude/hooks/ (Node scripts requiring packages from root node_modules/)

Reproduce

  1. In any project where .claude/hooks/ scripts require() packages from the project-root node_modules/ (gitignored, common pattern), run claude agents in the repo root.
  2. Spawn a /bg task (e.g. "list files in this directory").
  3. Observe: Claude Code creates a worktree at .claude/worktrees/<task-name>/, attempts its first tool use, hook fires inside the worktree, fails with node:internal/modules/cjs/loader:1459 MODULE_NOT_FOUND.

Trace excerpt observed in-session:

⏺ Creating worktree(mid-month-status)
Switched to worktree on branch worktree-mid-month-status
/Users/.../.claude/worktrees/mid-month-status

  Ran 1 shell command
Failed with non-blocking status code: node:internal/modules/cjs/loader:1459
Failed with non-blocking status code: node:internal/modules/cjs/loader:1459

Expected

Either:

  • /bg worktrees inherit node_modules (e.g. symlink from parent on worktree creation), or
  • Worktree creation is opt-in / configurable per project so users whose hooks aren't worktree-safe can disable it, or
  • Hooks run from the main worktree's cwd rather than the new bg worktree, so they can resolve node_modules relative to the original repo root.

Workaround

Manually symlink node_modules into each new worktree before tool calls — brittle, and the worktree is created automatically without an obvious pre-hook to do this.

Why it matters

Any project that uses Node-based hooks (a common pattern for custom session-start, pre-tool-use, etc. hooks) is currently incompatible with Agent View's background sessions. The first hook invocation in a bg task fails, the session can't recover, and the user has no way to know why until they click into the session and read the loader error.

View original on GitHub ↗

This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗