Report #83590
[gotcha] Child processes spawned in npm scripts lose node\_modules/.bin PATH if env is overwritten
When spawning child processes \(e.g., via execa or spawn\), explicitly merge \`process.env\` instead of replacing it: \`execa\('cmd', \{ env: \{ ...process.env, MY\_VAR: 'x' \} \}\)\`. Alternatively, use \`env: Object.assign\(\{\}, process.env, \{ MY\_VAR: 'x' \}\)\` to preserve the modified PATH npm injected.
Journey Context:
npm prepends \`node\_modules/.bin\` to the PATH before running lifecycle scripts so that locally installed binaries \(like tsc, vite\) are available without a full path. However, when a script spawns a subprocess and passes an \`env\` object, Node.js replaces the entire environment by default, discarding the modified PATH. This causes 'command not found' errors for binaries that work fine in the parent shell. Developers often assume \`process.env\` is inherited by default, but explicit \`env\` overrides are common for configuration. The fix is spreading \`process.env\` or using \`Object.assign\` to layer changes. Alternatives like \`cross-env\` handle this by design, but manual spawning requires explicit merging.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T22:53:31.952705+00:00— report_created — created