Error: Cannot find module (Node.js)

Node’s require or import looked for a file or package and the resolver came up empty. Usually install, path, or build-output — not a mysterious runtime bug.

What MODULE_NOT_FOUND means

Node tried to load a module ID and failed. Typical stack:

Error: Cannot find module 'express'
Require stack:
- /app/server.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:…)
    code: 'MODULE_NOT_FOUND'

ESM looks similar: Error [ERR_MODULE_NOT_FOUND]: Cannot find package '…' imported from … or Cannot find module '…' imported from ….

Checklist

  1. Dependency not installed — node_modules missing or incomplete (fresh clone, Docker layer, CI cache miss). From the directory that owns package.json:
    npm ci          # or: npm install / pnpm i / yarn
    node -e "console.log(require.resolve('express'))"
  2. Wrong working directory — you ran node src/index.js from a parent monorepo folder, so local ./lib/foo paths miss. cd into the package first.
  3. Forgot to build — node dist/server.js but dist/ was never emitted (tsc, nest build, next build). CI must run the build step before start.
  4. Relative path / extension
    • CJS: require('./util') resolves util.js, util.json, util/index.js.
    • ESM with "type": "module": Node often wants the explicit extension ./util.js (even if the source was .ts compiled to .js).
    • Case matters on Linux: ./Util.js ≠ ./util.js.
  5. Optional / peer dependency — a plugin expects peerDependencies you never installed. Read the package docs and install the peer.
  6. Native addons — Cannot find module './foo.node' after copying node_modules across OS/CPU. Rebuild: npm rebuild.

Node vs webpack / Next “Can't resolve”

ErrorWho failed
Cannot find module / MODULE_NOT_FOUNDNode runtime (or ts-node) resolver
Module not found: Can't resolveBundler at build time — see Can't resolve guide
ModuleNotFoundError: No module namedPython — see Python guide

Quick probes

pwd
ls package.json node_modules 2>/dev/null
node -p "process.versions.node"
node -e "console.log(module.paths)"
# ESM package check:
node -e "import('express').then(()=>console.log('ok')).catch(e=>console.error(e.code,e.message))"

Related

Module not found: Can't resolve · ModuleNotFoundError (Python) · npm ENOENT · Cannot use import statement outside a module · npm ERESOLVE

Questions

What does Error: Cannot find module mean?

Node could not resolve that module id to a file under node_modules or a relative path. Install dependencies or fix the path/build output.

I installed the package — why still MODULE_NOT_FOUND?

Install ran in a different folder than the process cwd, or you are running compiled files from an empty dist/. Confirm require.resolve from the same directory as the app.

Is this the same as webpack Can't resolve?

No. Can't resolve is the bundler at build time. Cannot find module is usually the Node runtime. Different guides, overlapping causes.

TypeScript: Cannot find module './foo'

Either path aliases only exist in tsconfig (not in Node), or you must compile first and import the .js output. ts-node/tsx and bundlers handle aliases differently.

ERR_MODULE_NOT_FOUND for a relative import

In ESM, add the file extension (./foo.js). Node will not guess .js the way TypeScript does.

Docker image fails but laptop works

The image likely skipped npm ci, used a .dockerignore that dropped node_modules without reinstalling, or copied host node_modules built for another platform.

Published .

Try it

Related guides

Newest guides