---
title: "Error: Cannot find module (MODULE_NOT_FOUND) in Node.js"
description: "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."
url: https://www.danny.engineering/answer/nodejs/error-cannot-find-module-module_not_found-in-nodejs
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T17:24:00.000Z
tags: ["Node.js"]
---

# Error: Cannot find module (MODULE_NOT_FOUND) in Node.js

## 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.


```bash
# A package is missing
npm install express

# Still broken? Reinstall cleanly
rm -rf node_modules
npm 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:


```bash
Error: Cannot find module './utils/format'
Require stack:
- /app/src/index.js
```


How Node resolves the string depends on its shape:

- **Bare names** such as `express` are looked up in `node_modules`, starting next to the importing file and walking up the folders.
- **Relative paths** such as `./utils/format` are 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:


```bash
npm ls express
npm 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.


```javascript
// src/routes/users.js
import { 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.


```diff
- import { format } from './utils/format';
+ import { format } from './utils/format.js';

- import routes from './routes';
+ 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:


```bash
npm run build
node 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 install` in a different folder from the one with `package.json`.
- Deleting `package-lock.json` as a first step. Try a clean `node_modules` reinstall before changing versions.
- Installing a package globally and expecting a project to find it. Project imports only look in `node_modules` folders, not the global one.
- Expecting TypeScript path aliases from `tsconfig.json` to work at runtime. Node does not read them; use `imports` in `package.json` or a bundler.
- Copying only `dist` into a Docker image and forgetting `node_modules` or the production install step.

## Related reading

- [Node.js: Modules, CommonJS](https://nodejs.org/api/modules.html)
- [Node.js: ECMAScript modules](https://nodejs.org/api/esm.html)
