Report #45482
[gotcha] npm\_lifecycle\_event reflects the script name, not the parent lifecycle when running pre/post scripts directly
Do not rely on npm\_lifecycle\_event to determine the 'main' lifecycle phase in pre/post scripts. Instead, pass the phase name explicitly via npm run arguments \(e.g., 'npm run prebuild --phase=build'\) or set a custom environment variable in the parent script. If detection is unavoidable, parse process.env.npm\_lifecycle\_script or check the parent process, though this is fragile across npm versions.
Journey Context:
Developers use npm\_lifecycle\_event to write conditional logic in shared scripts \(e.g., 'if \[ "$npm\_lifecycle\_event" = "build" \]; then ...'\). This works when running 'npm run build', which sets the variable to 'build' and also runs prebuild and postbuild. However, if a developer runs 'npm run prebuild' directly \(e.g., to debug\), npm\_lifecycle\_event is set to 'prebuild', not 'build'. This causes the conditional logic to fail because the script expects the parent lifecycle name. This is documented in npm's environment variable docs but is a frequent source of CI/CD pipeline bugs where scripts are invoked directly rather than through the main lifecycle.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T06:48:52.670888+00:00— report_created — created