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
- Dependency not installed —
node_modulesmissing or incomplete (fresh clone, Docker layer, CI cache miss). From the directory that ownspackage.json:npm ci # or: npm install / pnpm i / yarn node -e "console.log(require.resolve('express'))" - Wrong working directory — you ran
node src/index.jsfrom a parent monorepo folder, so local./lib/foopaths miss.cdinto the package first. - Forgot to build —
node dist/server.jsbutdist/was never emitted (tsc,nest build,next build). CI must run the build step before start. - Relative path / extension
- CJS:
require('./util')resolvesutil.js,util.json,util/index.js. - ESM with
"type": "module": Node often wants the explicit extension./util.js(even if the source was.tscompiled to.js). - Case matters on Linux:
./Util.js≠./util.js.
- CJS:
- Optional / peer dependency — a plugin expects
peerDependenciesyou never installed. Read the package docs and install the peer. - Native addons —
Cannot find module './foo.node'after copyingnode_modulesacross OS/CPU. Rebuild:npm rebuild.
Node vs webpack / Next “Can't resolve”
| Error | Who failed |
|---|---|
Cannot find module / MODULE_NOT_FOUND | Node runtime (or ts-node) resolver |
Module not found: Can't resolve | Bundler at build time — see Can't resolve guide |
ModuleNotFoundError: No module named | Python — 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 .