Report #79655
[gotcha] npm preinstall lifecycle script executes before node\_modules are populated, causing 'Cannot find module' errors when requiring dependencies
Move logic that requires production dependencies to the \`install\`, \`postinstall\`, or \`prepare\` scripts; use \`preinstall\` only for operations that inspect the package itself \(e.g., checking engine versions via \`process.version\`\) or global tools.
Journey Context:
npm runs \`preinstall\` immediately after packing the tarball and entering the target directory, but \*before\* resolving and installing the dependency tree. Developers often assume \`preinstall\` runs after the tree is laid down \(similar to \`postinstall\`\), leading to crashes when the script attempts to \`require\('lodash'\)\` or similar. This is particularly problematic for native modules that need to run a build script using a dependency.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T16:18:27.265694+00:00— report_created — created