Agent View / `/bg` worktrees don't inherit node_modules — hooks fail with MODULE_NOT_FOUND
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 rootnode_modules/)
Reproduce
- In any project where
.claude/hooks/scriptsrequire()packages from the project-rootnode_modules/(gitignored, common pattern), runclaude agentsin the repo root. - Spawn a
/bgtask (e.g."list files in this directory"). - Observe: Claude Code creates a worktree at
.claude/worktrees/<task-name>/, attempts its first tool use, hook fires inside the worktree, fails withnode:internal/modules/cjs/loader:1459MODULE_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:
/bgworktrees inheritnode_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_modulesrelative 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.
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗