Report #11846
[gotcha] npm 'preinstall' lifecycle script executes before dependencies are installed, causing 'module not found' errors when requiring packages
Never require\(\) npm dependencies inside preinstall scripts. Use preinstall only for system checks \(Node version, platform\) or file operations using only Node built-ins. Move any logic requiring dependencies to 'postinstall' or 'prepare', but note 'prepare' also runs on git dependency installation.
Journey Context:
Developers assume the environment is ready when scripts run. However, npm's lifecycle order runs 'preinstall' before installing the package's own dependencies \(listed in dependencies/devDependencies\). The node\_modules directory is empty or incomplete during preinstall. Common mistake: using 'rimraf' or 'fs-extra' \(dependencies\) in preinstall to clean directories. The fix requires understanding the specific timing: preinstall \(no deps\) -> install \(deps fetched\) -> postinstall \(deps available\) -> prepare.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T14:23:19.871579+00:00— report_created — created