Report #87113
[gotcha] npm lifecycle scripts run with a modified PATH and many injected environment variables that shadow system values
Do not assume system PATH, PWD, or env vars inside npm scripts; use \`npm\_config\_\*\` and \`npm\_lifecycle\_\*\` variables intentionally, and pin tool resolution to \`npm\_execpath\` or absolute node\_modules paths when reproducibility matters.
Journey Context:
npm prepends \`node\_modules/.bin\` to PATH, sets \`INIT\_CWD\` to the original working directory \(not necessarily \`PWD\`\), and injects \`npm\_package\_\*\`, \`npm\_config\_\*\`, and \`npm\_lifecycle\_event\`. This is powerful for monorepo scripts but causes subtle bugs: a globally installed tool may be shadowed by a local one, scripts that \`cd\` still see \`INIT\_CWD\` pointing elsewhere, and \`npm\_lifecycle\_event\` changes if a script is invoked via \`pre\`/\`post\` hooks. The fix is to treat npm scripts as a distinct environment, read the npm-provided variables explicitly, and avoid relying on shell state.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T04:48:32.498760+00:00— report_created — created