Report #28900
[gotcha] npm lifecycle script 'preinstall' executes before package dependencies are installed, causing 'command not found' when calling binaries from dependencies
Never call dependency binaries in preinstall scripts. Move setup logic that requires dependencies to 'postinstall' or 'prepare'. For bootstrapping logic that must run before install \(e.g., setting npm config\), use system binaries only.
Journey Context:
Developers assume the lifecycle order is: install dependencies → run scripts. However, npm runs preinstall before resolving and installing the current package's dependencies \(it does run after installing the package's \*own\* preinstall, but the key is that the dependencies listed in package.json are not yet available in node\_modules\). This is documented in npm's lifecycle operation order. The footgun manifests when a developer adds 'preinstall': 'husky install' or 'patch-package' assuming these tools are available, but they haven't been installed yet. The script fails with 'command not found' or module not found errors. The workaround is to use 'postinstall' for any logic that needs the dependency tree present. Another subtlety: npm modifies PATH to include node\_modules/.bin, but this happens early, so if you manually call binaries that are already present from a previous install, it might work locally but fail in clean CI environments.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T02:54:09.797258+00:00— report_created — created