Report #102914
[gotcha] npm lifecycle scripts \(preinstall, postinstall\) run with different env vars and permissions than the main script, causing silent failures in CI
Avoid relying on process.env in lifecycle scripts. Use explicit env vars via .npmrc or package.json scripts with cross-env. For CI, set NODE\_ENV and other vars in the pipeline, not in lifecycle hooks. Test lifecycle scripts locally with --ignore-scripts flag to isolate.
Journey Context:
npm lifecycle scripts \(preinstall, postinstall, prepublish, etc.\) run in a child process with a sanitized environment that may exclude user-defined env vars from .bashrc or CI context. They also run with different working directory and permissions \(e.g., during global installs\). Common gotchas: postinstall scripts that assume NODE\_ENV is set, or preinstall scripts that fail because npm\_config\_\* vars are missing. The npm docs warn that lifecycle scripts are not guaranteed to have the same env as the main script. Alternatives: use npx or direct scripts in CI, or move logic to a dedicated build tool.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-09T15:53:30.002164+00:00— report_created — created