Error: Cannot find module means Node could not resolve the name or path in a require() or import. For a package name, run npm install <package> in the folder that holds your package.json; for a relative path such as ./utils, check the spelling, the letter case and, with ES modules, the file extension.
The examples below were reproduced with Node.js 18 on Windows. Paths are shortened to C:\work\app. Newer Node versions print the same error codes with slightly different internal stack frames.
What the error looks like
There are two variants, depending on whether the code uses CommonJS require() or ES module import:
| Situation | Message | Code |
|---|---|---|
require("express"), package not installed | Error: Cannot find module 'express' | MODULE_NOT_FOUND |
node missing.js, entry file doesn't exist | Error: Cannot find module 'C:\work\app\missing.js' | MODULE_NOT_FOUND |
import x from "chalk", package not installed | Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'chalk' imported from C:\work\app\d.mjs | ERR_MODULE_NOT_FOUND |
import x from "./utils", extension missing | Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'C:\work\app\utils' imported from C:\work\app\c.mjs | ERR_MODULE_NOT_FOUND |
The full CommonJS version, as printed by Node 18 (example output, internal frames trimmed):
Error: Cannot find module 'express'
Require stack:
- C:\work\app\a.js
at Module._resolveFilename (node:internal/modules/cjs/loader:1077:15)
...
at Object.<anonymous> (C:\work\app\a.js:1:1) {
code: 'MODULE_NOT_FOUND',
requireStack: [ 'C:\\work\\app\\a.js' ]
}
Read two things from it. The quoted name ('express') tells you whether Node was looking for a package or a file path. The Require stack lists which of your files asked for it: here line 1 of a.js. If the stack shows a file inside node_modules, a dependency is missing one of its dependencies, which points to a broken install rather than your code.
Cause 1: the package isn't installed
This is the most common case: a fresh clone, a new machine, a CI job or a Docker image where npm install never ran, or a package you started using but never added. Confirm it:
npm ls express
Example output when it is missing:
[email protected] C:\work\app
`-- (empty)
npm ls exits with code 1 in this case. Fix it from the project root (the folder with package.json):
npm install # installs everything listed in package.json
npm install express # adds a package you forgot to declare
In CI and Dockerfiles, use npm ci. The npm docs say it requires a package-lock.json, deletes any existing node_modules first, and fails instead of silently updating if the lockfile and package.json disagree. That makes "works on my machine" differences show up as a clear install error. What package-lock.json does covers the lockfile side.
One production-only trap: npm skips devDependencies when NODE_ENV=production (the npm docs say omit defaults to dev then). If the server fails with Cannot find module for a package that works locally, check whether it is listed under devDependencies but needed at runtime, and move it to dependencies.
Cause 2: you ran node from the wrong folder
For a bare package name, Node does not look in your current terminal directory or in global installs. The Node docs describe the rule: it starts at the directory of the file doing the require(), adds /node_modules, and then moves to each parent directory "until the root of the file system is reached". You can print the exact list of folders Node will search:
node -e "console.log(require.resolve.paths('express'))"
Example output (first entries):
[
'C:\\work\\app\\node_modules',
'C:\\work\\node_modules',
'C:\\node_modules',
...
]
So a package installed in C:\work\other-project\node_modules is invisible to C:\work\app, and so is anything from npm install -g. If you have a monorepo or nested folders, make sure the package is installed in the node_modules next to (or above) the file that imports it.
The node missing.js variant is the same problem from another angle: the entry file path is resolved against your current directory. cd into the project or pass the correct path.
Cause 3: a wrong relative path
Anything starting with ./, ../ or / is a file path, resolved relative to the importing file, not to the folder you run node from. require("./utils") inside src/routes/users.js looks for src/routes/utils.js. If the file is in src/, you need ../utils.
With require(), Node tries the exact name, then adds .js, .json and .node in that order, and also accepts a folder with an index.js. Anything with another extension, such as .cjs or .ts, must be written in full.
Watch the letter case. On Windows, require("./Utils") loaded utils.js without complaint in our test, because the file system is case-insensitive. Linux file systems are case-sensitive, so the same code fails in a Linux container or CI runner. Make every import match the file name exactly.
Cause 4: ES modules need the file extension
If your file is .mjs, or package.json has "type": "module", Node uses the ESM resolver, which is stricter. The Node docs are explicit: "A file extension must be provided when using the import keyword to resolve relative or absolute specifiers. Directory indexes (e.g. './startup/index.js') must also be fully specified."
import { a } from "./utils"; // ERR_MODULE_NOT_FOUND
import { a } from "./utils.js"; // works
import { b } from "./lib"; // fails: no folder index lookup
import { b } from "./lib/index.js"; // works
This is the usual cause when code that ran fine with require() breaks after switching to import. The ESM resolver also ignores NODE_PATH, so setups that relied on it stop working too.
Cause 5: TypeScript path aliases and build output
An import like import { db } from "@/lib/db" type-checks because of the paths option in tsconfig.json, then fails at runtime with Cannot find module '@/lib/db'. The TypeScript docs explain why: paths "does not change how import paths are emitted by tsc", and should only be used to tell TypeScript about a mapping that "another tool" applies at runtime or when bundling. Plain node dist/index.js knows nothing about it. Your options:
- Let a bundler or framework apply the aliases (most front-end and full-stack frameworks do).
- Use Node's own subpath imports (names starting with
#, declared inpackage.jsonunder"imports"), which Node resolves natively. - Use relative imports.
A related mistake is running the wrong file: node src/index.ts on an old Node, or node dist/index.js before running the build, so dist/ doesn't exist yet. Check that the file in the error message exists on disk.
If it still fails
Do a clean reinstall from the lockfile: delete node_modules, then run npm ci (or npm install if there is no lockfile). Check that you are on the Node version the project expects (node --version, and the engines field or .nvmrc if present). If the missing module is inside node_modules (the require stack points there), look at whether that dependency's own package.json lists it; if not, it is a bug in that package, and upgrading or pinning a different version is the fix. The Python equivalent of this error has its own causes; see ModuleNotFoundError in Python.
Frequently asked questions
- What is the difference between MODULE_NOT_FOUND and ERR_MODULE_NOT_FOUND?
- MODULE_NOT_FOUND comes from require() (CommonJS) and ERR_MODULE_NOT_FOUND comes from import (ES modules). Both mean Node could not resolve the name or path you gave it. The ESM resolver is stricter: it does not add file extensions or look for index.js, so a path that works with require() can fail with import.
- Should I delete node_modules and reinstall?
- It fixes a corrupted or half-finished install, but try npm install first, and run npm ls <package> to see whether the package is listed at all. If you do reinstall, delete node_modules and run npm ci when a package-lock.json exists, so you get exactly the locked versions.
- Why does it work on my Windows machine but fail on Linux or in Docker?
- Usually letter case. Windows and macOS file systems are normally case-insensitive, so require('./Utils') finds utils.js there, but Linux file systems are case-sensitive and the same line fails. Make the import match the file name exactly.
- Does installing a package globally fix Cannot find module?
- Not for require() or import in your project. Node looks for packages in node_modules folders starting from the importing file's directory and walking up; a global npm install puts packages elsewhere. Install the package as a project dependency instead.