Skip to content
Latchkey

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.

node
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

Terminal
npm install -D webpack webpack-cli
npm ls webpack

Install dev deps for the build step

  1. Do not pass --omit=dev/--production to the install that precedes the build.
  2. Unset NODE_ENV=production for the install step, or move webpack out of devDependencies only if you truly need it at runtime.
  3. 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 ci so the installed tree matches the lockfile.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →