claude-code - 💡(How to fix) Fix Agent View / `/bg` worktrees don't inherit node_modules — hooks fail with MODULE_NOT_FOUND

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…

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.

Error Message

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.

Root Cause

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.

Fix Action

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.

Code Example

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
RAW_BUFFERClick to expand / collapse

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.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING