webpack "Cannot find module 'webpack'" - Fix Missing Dep in CI
Node tried to load webpack and it is not in node_modules. The build command runs webpack, but the package was never declared, or was dropped by a production-only install in CI.
What this error means
The build step fails immediately with Error: Cannot find module 'webpack' (or the CLI). It is deterministic - a missing dependency, not flake.
Error: Cannot find module 'webpack'
Require stack:
- /app/node_modules/.bin/webpack
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1145:15)Common causes
webpack not in package.json
A framework or template invoked webpack but the package itself was never added to devDependencies, so a clean npm ci does not install it.
devDependencies pruned in CI
Installing with --production/--omit=dev (or NODE_ENV=production) skips devDependencies, so webpack is absent when the build runs.
How to fix it
Declare webpack as a dev dependency
npm install -D webpack webpack-cli
npm ls webpackInstall dev deps for the build step
- Do not pass
--omit=dev/--productionto the install that precedes the build. - Unset
NODE_ENV=productionfor the install step, or move webpack out of devDependencies only if you truly need it at runtime. - Run
npm ci(with dev deps) so the build toolchain is present.
How to prevent it
- Declare every build tool you invoke in
devDependencies. - Keep dev dependencies installed for build steps in CI.
- Use
npm ciso the installed tree matches the lockfile.