Node throws Cannot find module when it cannot resolve a path or package. Learn how to read the error, then fix missing installs, wrong paths, file extensions and missing builds.
The short answer
#Node could not find the file or package you asked for. Read the quoted name in the error: if it is a package name, install it; if it is a path, check that the file exists relative to the file doing the import, with the right case and extension.
_10# A package is missing_10npm install express_10_10# Still broken? Reinstall cleanly_10rm -rf node_modules_10npm install
Why it happens
#Every require() or import goes through Node's module resolution. It turns the string you wrote into a file on disk, and when nothing matches it throws. In CommonJS the error code is MODULE_NOT_FOUND; in ES modules it is ERR_MODULE_NOT_FOUND. The message tells you what was being resolved and which file asked for it:
_10Error: Cannot find module './utils/format'_10Require stack:_10- /app/src/index.js
How Node resolves the string depends on its shape:
- Bare names such as
expressare looked up innode_modules, starting next to the importing file and walking up the folders. - Relative paths such as
./utils/formatare resolved from the importing file's folder, not from where you ran the command. - Absolute paths are used as they are.
So the usual causes are a package that was never installed, a typo or wrong folder in a relative path, a missing extension in an ES module, or a package whose entry file does not exist yet.
How to fix it
#Install the missing package
If the quoted name has no ./ in front, it is a package. Check it is in package.json and installed:
_10npm ls express_10npm install express
In a monorepo, make sure you installed from the workspace root and that the package is listed in the dependencies of the workspace that uses it.
Check the relative path
Relative imports are resolved from the file that contains them. Check the spelling and the case: macOS and Windows are usually case insensitive, but Linux servers and CI are not, so ./Utils and ./utils behave differently in production.
_10// src/routes/users.js_10import { format } from '../utils/format.js'; // src/utils/format.js
Add file extensions in ES modules
CommonJS tries .js, .json and index.js for you. ES modules do not. You must write the full file name, including the extension, and point at a file rather than a folder.
_10- import { format } from './utils/format';_10+ import { format } from './utils/format.js';_10_10- import routes from './routes';_10+ import routes from './routes/index.js';
Run the command from the right place
If the missing module is the script itself, such as Cannot find module '/app/server.js', you are running node from the wrong folder or the file name is wrong. Check with pwd and ls, or use the scripts in package.json, which always run from the package root.
Build first if the entry is in dist
If the error points to something like dist/index.js, the package's main or exports field points to compiled output that does not exist yet. Run the build:
_10npm run build_10node dist/index.js
Use exported paths only
A package with an exports field only lets you import the paths it lists. Importing a deep path such as some-lib/lib/internal.js fails even though the file exists. Use the public entry point from the package's docs.
Common pitfalls
#- Running
npm installin a different folder from the one withpackage.json. - Deleting
package-lock.jsonas a first step. Try a cleannode_modulesreinstall before changing versions. - Installing a package globally and expecting a project to find it. Project imports only look in
node_modulesfolders, not the global one. - Expecting TypeScript path aliases from
tsconfig.jsonto work at runtime. Node does not read them; useimportsinpackage.jsonor a bundler. - Copying only
distinto a Docker image and forgettingnode_modulesor the production install step.
Related reading
#Was this resource helpful?