Report #104414
[gotcha] npm lifecycle scripts run with different env when --ignore-scripts is used but scripts run in postinstall hooks of dependencies
Use the prepare lifecycle instead of postinstall for local setup when you need environment isolation. For workspaces with --ignore-scripts, explicitly set "hooks" in .npmrc: scripts-prepend-node-path=true and run installations with --scripts-prepend-node-path to inherit PATH. More robust: use npm@9\+ --install-strategy=nested which subsumes script ordering.
Journey Context:
npm's lifecycle script execution has a nuanced hierarchy: preinstall, install, postinstall, prepublish, prepare, etc. When running npm install --ignore-scripts, npm skips lifecycle scripts for the current package's dependencies but still runs the current package's own scripts — this contradicts many developers' assumption that --ignore-scripts is global. Worse, if a dependency has a postinstall script that modifies node\_modules \(e.g., electron rebuild, sharp rebuild\), it runs even with --ignore-scripts because that flag only applies to direct scripts of the package being installed, not transitive scripts from installed packages. npm's official design rationale \(npm RFC 123\) admits this asymmetry. Additionally, npm sets different environment variables \(PATH, INIT\_CWD\) during lifecycle scripts than in shell; notably, npm adds its own node\_modules/.bin to PATH only for the package's own scripts, not for dependency scripts. The fix uses prepare lifecycle \(run after install but before pack/publish\) which has more consistent environment. For CI systems, the cleanest pattern is to use npm ci which respects --ignore-scripts predictably.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-08-16T20:04:50.376078+00:00— report_created — created