Skip to main content

Node.js · 5 common causes

Cannot find module

Updated

The CommonJS loader could not resolve a require() — either the package is not installed or the relative path is wrong.

The exact message

node:internal/modules/cjs/loader:1143
  throw err;
  ^

Error: Cannot find module 'express'
Require stack:
- /app/server.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1140:15)
    at Module._load (node:internal/modules/cjs/loader:981:27) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/app/server.js' ]
}

Paths, line numbers and stack frames differ on every machine, and the wording is tightened between versions. The part that identifies the problem is Cannot find module.

How to fix it

Cause 1 of 5

Dependencies were never installed, or node_modules is out of date.

Run npm install locally, or npm ci in CI and after switching branches — npm ci installs exactly the lockfile and is reproducible. Then confirm the package is listed in package.json rather than only present by accident.

Cause 2 of 5

The package is in devDependencies but the environment installed production only.

npm ci --omit=dev and NODE_ENV=production npm install both skip devDependencies. If the module is needed at runtime, move it into dependencies — that is the fix, not reinstalling with dev dependencies in production.

Cause 3 of 5

The specifier is missing its ./ prefix.

require('utils') searches node_modules for a package called utils; require('./utils') looks beside the current file. One character decides which.

Cause 4 of 5

The build output is missing from the image.

Check that dist/ actually contains the compiled file and that the Dockerfile copies it. Building before npm ci, or copying source over the build output in a later layer, produces exactly this error at boot.

Cause 5 of 5

The case of the path does not match the file.

This passes on macOS and Windows and fails on Linux. Rename the require to match the file byte for byte; git mv the file if the repository itself has the wrong case, since a plain rename on a case-insensitive filesystem is not recorded.

Stopping it coming back

Commit the lockfile, install with npm ci everywhere except when deliberately adding a package, and build container images on Linux so case problems surface immediately.

Cannot find module — common questions

Should I delete node_modules and reinstall?
It resolves a genuinely corrupted tree, but try npm ci first — it removes and recreates node_modules from the lockfile anyway, and it tells you if the lockfile and package.json disagree, which a manual delete hides.
Why does the error name a package I never installed?
It is a transitive dependency of something you did install, and its own install failed or was pruned. npm ls <name> prints the chain that requires it.
Can I commit node_modules to avoid this?
No. It bloats the repository, breaks on any package with compiled native bindings built for a different platform, and hides the lockfile problems that cause the error. A committed lockfile plus npm ci gives the same determinism without any of that.

Test the fix before you paste it back

Reproducing the failure in a scratch file is usually faster than editing the real one. AltF Code is a free in-browser editor with real syntax highlighting for HTML, CSS, JavaScript, TypeScript, Python, SQL and JSON, and it runs HTML, CSS and JavaScript live in a preview panel. No install and no sign-up.

Open the editor