How to fix "Error: Cannot find module" in Node.js
Tested: we reproduced this error and confirmed the fix on Node 22.23.5 on Ubuntu 24.04, November 29, 2026.
Node looked for the module you named and didn’t find it. The message tells you which kind of lookup failed:
| The error says | What it means | Fix |
|---|---|---|
Cannot find module 'express' |
A package isn’t installed | npm install express |
Cannot find module './util' |
A relative path is wrong | Fix the path from the file doing the import |
Cannot find module '/app/srv.js' |
The file you ran doesn’t exist | Check the name, or cd into the project first |
ERR_MODULE_NOT_FOUND ... Did you mean to import "./src/m.js"? |
An ES module import without the extension | Add .js to the import |
We reproduced all four on Node 22.23.5. Each fix in the right-hand column made the code run.
Fix: the package isn’t installed
Error: Cannot find module 'express'
Require stack:
- /home/you/project/index.js
Install it in the project that’s running:
npm install express
npm ls express # confirms it's installed and which version
If it is listed in package.json but missing, run npm install (or npm ci in CI). In production builds, check it’s in dependencies, not devDependencies.
Fix: reinstall node_modules from scratch
If the package is listed in package.json and still can’t be found, the install is probably incomplete or was made for another Node version or operating system. Delete it and reinstall:
rm -rf node_modules package-lock.json
npm install
On Windows PowerShell, use Remove-Item -Recurse -Force node_modules, package-lock.json. Deleting package-lock.json lets npm pick newer versions within your ranges; keep it if you need the exact versions, and delete only node_modules. All five of the top-ranking guides suggest this step. npm cache clean --force is rarely needed; try it only if the reinstall itself fails with cache errors.
Fix: a relative path is wrong
Relative paths in require and import start from the file doing the import, not from where you ran node. In our test require('./util') failed from the project root because the file was src/util.js; require('./src/util') worked.
Two quick checks:
- Copy the path from the error and run
lson it. - Match the case exactly.
./Utiland./utilare different files on Linux, even if your Mac treats them as the same.
Fix: you’re in the wrong folder
node srv.js resolves srv.js from your current folder. Our test from /tmp failed with Cannot find module '/tmp/app.js'. Either cd into the project or give the full path. In package.json scripts, npm runs from the folder that holds package.json, so "start": "node src/server.js" works from anywhere in the project.
Fix: ES modules need the extension
With "type": "module", Node won’t guess file extensions. import m from './src/m' failed with:
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/home/you/project/src/m' imported from /home/you/project/e.js
Did you mean to import "./src/m.js"?
Write import m from './src/m.js'. If the error is about import itself, see Cannot use import statement outside a module.
Fix: it only fails in tests or after a TypeScript build
- Jest:
npm testruns from the project root and uses its own resolver. Path aliases such as@/utilsneed a matchingmoduleNameMapperentry in the Jest config, or every import that uses them fails withCannot find module. - TypeScript:
pathsintsconfig.jsononly tell the type checker where files are. The compiled JavaScript still contains@/utils, so Node can’t find it at run time. Use relative imports, a bundler or a runtime resolver such astsconfig-paths.
We didn’t reproduce these two; they come from the questions searchers ask alongside this error. For the Python version of the error, see ModuleNotFoundError: No module named.
Confirm it worked
Run the same command. The script should start without the error. If a different module is now missing, repeat the steps for that one; a fresh clone missing many packages just needs npm install.
FAQ
Should I delete node_modules?
Only after the simple checks. npm install restores it from package.json and the lockfile. Deleting it helps when an install was interrupted or you switched Node versions and a native package needs rebuilding.
Why does it work locally but fail in Docker or CI?
Usually the dependency is in devDependencies and the build installs production packages only, or the file name's case differs and the server's file system is case-sensitive.
What does MODULE_NOT_FOUND mean?
It's the error code Node attaches to this error for require. ES modules use ERR_MODULE_NOT_FOUND for the same problem.
