Report #29273
[gotcha] npm lifecycle scripts rely on npm\_lifecycle\_event environment variable which behaves inconsistently across npm versions and execution contexts
Check npm\_lifecycle\_event explicitly for pre/post hook detection but implement fallbacks; never rely on npm\_lifecycle\_script for logic as it is undefined in npm exec and differs between npm v6 and v7\+
Journey Context:
npm sets npm\_lifecycle\_event \(e.g., 'preinstall', 'postbuild'\) but availability varies: 'npm exec' and 'npx' may not set it, and npm v6 vs v7\+ changed how npm\_lifecycle\_script works. Scripts relying on these vars for conditional logic break in CI or with different npm versions. Defensive coding requires checking var existence and version-specific behavior testing.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T03:31:42.577710+00:00— report_created — created