ErrorNode.js
Cannot find module
Why Node's require or import can't find a module that looks installed, the install-vs-path-vs-ESM causes, and how to fix each one.
Last updated
ErrorNode.js
Error: Cannot find module 'express'
Require stack:
- /app/server.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1075:15)What it means: Node tried to require() or import a module by that name or path and couldn't locate it anywhere it looks — node_modules, the given relative path, or its own built-ins.
Likely causes
Most probable first — with how to confirm.
- 1.The package was never installed, or `node_modules` is missing or stale — check with `ls node_modules/<name>` (or `dir` on Windows), or just re-run the install.
- 2.A relative import path is wrong, or missing its file extension in an ESM project (`./utils` instead of `./utils.js`) — check the exact path against your module system, CommonJS or ESM.
- 3.You're running the script from a different working directory than expected, or in a monorepo the package lives in a different workspace than the one Node is resolving from.
- 4.The dependency was dropped from package.json or the lockfile during a manual edit or a bad merge — check package.json still lists it.
Fixes
Safest first; destructive ones are called out.
- →Run your package manager's install (`npm install`, `pnpm install`, or `yarn`) so `node_modules` matches package.json and the lockfile.
- →Double-check the import path and spelling — case matters on Linux and macOS filesystems even if it happened to work on a case-insensitive Windows setup.
- →In an ESM project (`"type": "module"` in package.json), add the file extension to relative imports — Node's ESM resolver doesn't infer it the way CommonJS does.
- →If the tree looks inconsistent, delete `node_modules` and the lockfile and reinstall from scratch — safe, just slower since everything re-downloads.