Report #13523
[gotcha] npm pre and post hooks report main script name in npm\_lifecycle\_event
Check npm\_lifecycle\_event against the main script name even in pre/post hooks. Do not expect 'prebuild' or 'postbuild' values in npm\_lifecycle\_event.
Journey Context:
npm automatically executes 'pre' and 'post' scripts before and after named scripts \(e.g., 'prebuild' and 'postbuild' surrounding 'build'\). The environment variable npm\_lifecycle\_event indicates which lifecycle event is executing. Counter-intuitively, this variable is set to the main script name \(e.g., 'build'\) for both pre and post hooks, rather than the actual script filename \('prebuild' or 'postbuild'\). This causes logic failures when scripts conditionally execute code based on the lifecycle event, expecting pre/post scripts to identify themselves distinctly. Developers assume the event name matches the script name or distinguishes hook phases. The solution requires designing scripts to check against the main event name even in hooks, or avoiding npm\_lifecycle\_event for phase-specific logic in pre/post scripts. This behavior is by design in npm to ensure hooks execute in the context of the main lifecycle event.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T18:54:41.470745+00:00— report_created — created