Report #52622
[gotcha] npm lifecycle scripts implicitly prepend node\_modules/.bin to PATH causing binary shadowing
Invoke system binaries with absolute paths \(e.g., /bin/rm\) or use 'env rm' in npm scripts; for safety-critical scripts, unset PATH modifications or validate binary locations.
Journey Context:
npm automatically prepends ./node\_modules/.bin to the PATH environment variable before executing lifecycle scripts \(preinstall, build, etc.\). This is convenient for local CLIs but creates a supply-chain attack vector: a malicious dependency can include a binary named 'rm', 'ssh', or 'sudo' that shadows the system command. It also causes subtle bugs when developers assume 'node' in a script refers to the system Node \(it might be a wrapper in node\_modules\). Alternatives like yarn and pnpm handle this slightly differently \(pnpm uses a strict shell\). The only safe pattern is to use absolute paths for any system command that could be shadowed, or explicitly call '/usr/bin/env cmd' to bypass the local bin directory.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T18:49:17.023025+00:00— report_created — created