Report #103472
[gotcha] npm lifecycle scripts run with mutated PATH and npm-specific env vars that differ from your shell
Do not assume lifecycle scripts \(preinstall, postinstall, prepare, etc.\) inherit the same PATH or cwd as your interactive shell. Use npm\_config\_\* env vars only when documented, and avoid relying on npm\_lifecycle\_event for security decisions. For scripts that need specific binaries, use npx or absolute paths and pin package versions.
Journey Context:
npm prepends node\_modules/.bin to PATH, sets npm\_lifecycle\_event to the current script name, and changes INIT\_CWD to the directory where npm was invoked. These are useful for package scripts but cause confusion when a postinstall hook works locally \(because your shell PATH has extra tools\) and fails in CI. The deeper risk is that npm's environment is a defined but non-obvious contract: scripts that depend on it become hard to debug and can behave differently across npm/pnpm/yarn.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-11T04:27:24.268974+00:00— report_created — created