claude-code - 💡(How to fix) Fix [BUG] PreToolUse/PostToolUse hook error: Cannot find module session-state.cjs [1 comments, 2 participants]

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…
GitHub stats
anthropics/claude-code#54743Fetched 2026-04-30 06:37:15
View on GitHub
Comments
1
Participants
2
Timeline
5
Reactions
0
Timeline (top)
labeled ×4commented ×1

Error Message

PreToolUse:Bash hook error Failed with non-blocking status code:

node:internal/modules/cjs/loader:1252 throw err; ^

Error: Cannot find module '/Users/uditgoenka/Desktop/workspace/social/.claude/hooks/session-state.cjs' at Function._resolveFilename (node:internal/modules/cjs/loader:1249:15) at Function._load (node:internal/modules/cjs/loader:1075:27) at TracingChannel.traceSync (node:diagnostics_channel:315:14) at wrapModuleLoad (node:internal/modules/cjs/loader:218:24) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:170:5) at node:internal/main/run_main_module:36:49 { code: 'MODULE_NOT_FOUND', requireStack: [] }

Code Example

PreToolUse:Bash hook error    Failed with non-blocking status code:

  node:internal/modules/cjs/loader:1252
    throw err;
    ^

  Error: Cannot find module '/Users/uditgoenka/Desktop/workspace/social/.claude/hooks/session-state.cjs'
      at Function._resolveFilename (node:internal/modules/cjs/loader:1249:15)
      at Function._load (node:internal/modules/cjs/loader:1075:27)
      at TracingChannel.traceSync (node:diagnostics_channel:315:14)
      at wrapModuleLoad (node:internal/modules/cjs/loader:218:24)
      at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:170:5)
      at node:internal/main/run_main_module:36:49 {
    code: 'MODULE_NOT_FOUND',
    requireStack: []
  }

---

{
  "hooks": {
    "Stop": [{ "hooks": [{ "type": "command", "command": "node \"$CLAUDE_PROJECT_DIR\"/.claude/hooks/session-state.cjs" }] }]
  }
}
RAW_BUFFERClick to expand / collapse

Preflight Checklist

  • I have searched existing issues and this hasn't been reported yet
  • This is a single bug report (please file separate reports for different bugs)
  • I am using the latest version of Claude Code

What happened

Multiple hook events (PreToolUse:Bash, PreToolUse:Read, PostToolUse:Bash, and the stop hook) throw a MODULE_NOT_FOUND error referencing a session-state.cjs file that doesn't exist in the project's .claude/hooks/ directory.

The error fires on nearly every tool call, flooding the output with non-blocking warnings:

PreToolUse:Bash hook error    Failed with non-blocking status code:

  node:internal/modules/cjs/loader:1252
    throw err;
    ^

  Error: Cannot find module '/Users/uditgoenka/Desktop/workspace/social/.claude/hooks/session-state.cjs'
      at Function._resolveFilename (node:internal/modules/cjs/loader:1249:15)
      at Function._load (node:internal/modules/cjs/loader:1075:27)
      at TracingChannel.traceSync (node:diagnostics_channel:315:14)
      at wrapModuleLoad (node:internal/modules/cjs/loader:218:24)
      at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:170:5)
      at node:internal/main/run_main_module:36:49 {
    code: 'MODULE_NOT_FOUND',
    requireStack: []
  }

Expected behavior

If a hook references a file that doesn't exist, Claude Code should either:

  1. Skip the hook silently (or with a single one-time warning), OR
  2. Validate hook paths at startup and report missing files once, not on every tool call

Actual behavior

The error fires on every PreToolUse, PostToolUse, and Stop event — dozens of times per session — cluttering the output. The hook is configured in global settings (~/.claude/settings.json) but references a per-project path ($CLAUDE_PROJECT_DIR/.claude/hooks/session-state.cjs) that doesn't exist in every project.

How to reproduce

  1. Add a hook in ~/.claude/settings.json:
{
  "hooks": {
    "Stop": [{ "hooks": [{ "type": "command", "command": "node \"$CLAUDE_PROJECT_DIR\"/.claude/hooks/session-state.cjs" }] }]
  }
}
  1. Open Claude Code in a project that does NOT have .claude/hooks/session-state.cjs
  2. Run any command — observe the error fires on every tool call

Suggested fix

  • Hooks referencing $CLAUDE_PROJECT_DIR paths should gracefully handle missing files
  • A single warning at session start ("Hook file not found: X — skipping") would be much better than per-event errors
  • Or: resolve $CLAUDE_PROJECT_DIR at registration time and skip hooks when the target file doesn't exist

Environment

  • Claude Code version: 2.1.123
  • Platform: macOS (Darwin 25.3.0)
  • Node.js: v22.11.0
  • Shell: zsh

extent analysis

TL;DR

The issue can be fixed by modifying the hook configuration to handle missing files or by validating hook paths at startup.

Guidance

  • Check the ~/.claude/settings.json file for hooks referencing $CLAUDE_PROJECT_DIR paths and consider removing or modifying them to handle missing files.
  • Validate hook paths at startup by resolving $CLAUDE_PROJECT_DIR and skipping hooks when the target file doesn't exist.
  • Consider adding a single warning at session start for missing hook files instead of firing errors on every tool call.
  • Review the session-state.cjs file path in the hook configuration to ensure it exists in the project's .claude/hooks/ directory.

Example

No code snippet is provided as the issue is related to configuration and file paths.

Notes

The issue is specific to the Claude Code version 2.1.123 and may not be applicable to other versions. The solution may require modifications to the Claude Code codebase or configuration files.

Recommendation

Apply a workaround by modifying the hook configuration to handle missing files or by validating hook paths at startup, as the issue is not fixed in the current version of Claude Code.

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…

FAQ

Expected behavior

If a hook references a file that doesn't exist, Claude Code should either:

  1. Skip the hook silently (or with a single one-time warning), OR
  2. Validate hook paths at startup and report missing files once, not on every tool call

Still need to ship something?

×6

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

Back to top recommendations

TRENDING