Report #99202
[gotcha] npm run build unexpectedly runs prebuild and postbuild, and process.env.npm\_lifecycle\_event reports the current hook stage, not the script the user typed.
Name your scripts carefully: any script 'pre' or 'post' will run automatically around 'npm run '. If you need to know which lifecycle stage is executing, read npm\_lifecycle\_event, but design scripts so each stage has a single responsibility; do not rely on it for user-facing command names.
Journey Context:
npm automatically expands any script into a pre/main/post sequence, including user-defined scripts. This is powerful for build pipelines but surprising when a typo like 'prebuild' quietly runs before every build. In lifecycle scripts, npm also sets environment variables such as npm\_lifecycle\_event and npm\_lifecycle\_script; these reflect the currently executing stage \(e.g., 'prepare', 'postinstall'\), not necessarily the top-level command the user invoked. Using npm\_lifecycle\_event to branch logic can make scripts hard to follow and differs across package managers \(yarn/pnpm have their own lifecycle rules\). Prefer explicit script composition or a task runner when the order matters, and reserve npm lifecycle hooks for well-known conventions.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-29T04:44:10.567626+00:00— report_created — created