Skip to content

fix: export PATH in hooks so node resolves on nvm systems#1

Open
frank-ih wants to merge 2 commits into
mainfrom
fix/nvm-node-path-in-hooks
Open

fix: export PATH in hooks so node resolves on nvm systems#1
frank-ih wants to merge 2 commits into
mainfrom
fix/nvm-node-path-in-hooks

Conversation

@frank-ih
Copy link
Copy Markdown
Owner

@frank-ih frank-ih commented Apr 4, 2026

Closes thedotmack#1598

🔴 Failing scenario (the bug)

On any system where Node is installed via nvm (not in a system path), every claude-mem hook fails silently:

Ran a hook on session startup
/bin/sh: node: command not found

Ran a hook on session startup
/bin/sh: node: command not found

Ran a hook on session compact
/bin/sh: node: command not found

Why: Claude Code executes hooks via /bin/sh, which launches a minimal non-interactive shell. It does not source .bashrc/.zshrc and does not inherit the user's $PATH. When nvm is used, node lives at ~/.nvm/versions/node/<version>/bin/ — a path that /bin/sh knows nothing about.

🟢 Expected behavior (after this fix)

All hooks resolve node correctly regardless of whether it was installed via nvm, Homebrew (/opt/homebrew/bin), or a system package (/usr/local/bin). No errors on session startup, compact, or stop.

✅ The fix

Each hook command now opens with a dynamic export PATH that:

  1. Detects whether ~/.nvm/versions/node exists
  2. If yes, resolves the latest installed version via sort -V | tail -1 and prepends its bin/ to PATH
  3. Also prepends /opt/homebrew/bin and /usr/local/bin for Homebrew and system installs
  4. Falls back cleanly on systems where node is already on PATH (no-op)
export PATH="$([ -d "$HOME/.nvm/versions/node" ] && echo "$HOME/.nvm/versions/node/$(ls "$HOME/.nvm/versions/node" | sort -V | tail -1)/bin:" || true)/usr/local/bin:/opt/homebrew/bin:/usr/bin:/bin:$PATH"

This pattern was already present in the cached version of hooks.json generated post-install, but was missing from the marketplace version that Claude Code actually reads at runtime.

🔁 Files changed

  • plugin/hooks/hooks.json — added export PATH prefix to all 6 hook commands (SessionStart ×3, UserPromptSubmit, PostToolUse, Stop, SessionEnd)

frank-ih added 2 commits April 5, 2026 00:07
…#1598)

Hooks run via /bin/sh which does not inherit the user shell PATH. On nvm setups node lives outside standard system paths, causing every hook to fail with: /bin/sh: node: command not found

The fix dynamically resolves the nvm node bin directory at runtime and prepends it to PATH along with common node locations (homebrew, /usr/local/bin). Falls back gracefully on systems where node is already on PATH.
Addresses CodeRabbit review: the previous fix always prepended the nvm path even when node was already resolvable, which could silently switch Node versions.

Now wraps the export in `command -v node >/dev/null 2>&1 || ...` so PATH is only augmented when node cannot already be found — a true no-op on systems where node is on PATH.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

hooks.json missing export PATH causes node: command not found on nvm setups

1 participant