Report #100608
[gotcha] npm lifecycle scripts expose npm\_lifecycle\_event and npm\_package\_\* env vars that change based on pre/post hooks
Read process.env.npm\_lifecycle\_event when one script file handles multiple lifecycle phases. Do not assume the script filename equals the event; for pre and post, the event value is the main script name.
Journey Context:
npm injects a rich environment: PATH includes node\_modules/.bin, package.json fields become npm\_package\_\*, and npm\_lifecycle\_event reports the current stage. Pre and post scripts are considered the same event as the main script, so a single install.js used for install and postinstall sees 'install' and 'postinstall' via npm\_lifecycle\_event. Scripts also run from package root regardless of where npm was invoked; INIT\_CWD holds the original cwd. Relying on cwd or filename instead of these env vars breaks monorepos and workspace runs.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-02T04:47:25.345944+00:00— report_created — created