Skip to content

SessionStart/PreCompact hooks fail (exit 127) when cwd has no package.json - npm --prefix reads package.json from cwd on Windows #16

Description

@gulecferit

Summary

The SessionStart and PreCompact hooks fail with a non-blocking error on every session start when the current working directory does not contain a package.json (e.g. a monorepo root or a non-Node project). The user sees this on every startup:

SessionStart:startup hook error
Failed with non-blocking status code: No stderr output

Root cause

hooks/hooks.json runs:

npm install --silent --prefix ${CLAUDE_PLUGIN_ROOT} --omit=dev --no-audit --no-fund >/dev/null 2>&1 && node ${CLAUDE_PLUGIN_ROOT}/hooks/session-start.js

On Windows (npm 10.9.x, Node 22), npm install --prefix <dir> still tries to read package.json from the cwd, not from the prefix dir. When the cwd has no package.json, npm fails:

npm error code ENOENT
npm error syscall open
npm error path C:\Users\<user>\<project>\package.json
npm error enoent Could not read package.json: Error: ENOENT: no such file or directory

exit code 127. Because the command chains with &&, session-start.js never runs — so besides the error banner, the compact-state injection is silently skipped. 2>&1 >/dev/null swallows stderr, hence "No stderr output", which makes this hard to diagnose.

(Presumably worse in the other direction too: when the cwd does have a package.json, npm may resolve the install against the user's project instead of the plugin dir.)

Fix that works

cd into the plugin root instead of relying on --prefix, and don't let an npm hiccup kill the context injection once deps exist:

"command": "cd \"${CLAUDE_PLUGIN_ROOT}\" && npm install --silent --omit=dev --no-audit --no-fund >/dev/null 2>&1; node \"${CLAUDE_PLUGIN_ROOT}/hooks/session-start.js\""

Verified locally on Windows 11 / npm 10.9.8: original command exits 127 from a cwd without package.json; patched command exits 0 and session-start.js runs.

Related: #14 (npm install on every invocation — fixing that would also shrink this failure surface) and #13 (quoting ${CLAUDE_PLUGIN_ROOT}, folded into the snippet above).

Environment

  • Windows 11 Pro, PowerShell/Git Bash
  • Node 22 (C:\Program Files\nodejs), npm 10.9.8
  • morph-compact 0.2.8 via marketplace
  • Project root without package.json (app lives in a subdirectory)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions