Skip to main content

Node.js · 5 common causes

ERR_MODULE_NOT_FOUND

Updated

Node's ES module loader followed an import to a file path that does not exist on disk.

The exact message

node:internal/modules/esm/resolve:265
    throw new ERR_MODULE_NOT_FOUND(
          ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/app/src/utils' imported from /app/src/index.js
    at finalizeResolution (node:internal/modules/esm/resolve:265:11)
    at moduleResolve (node:internal/modules/esm/resolve:933:10)
  code: 'ERR_MODULE_NOT_FOUND'
}

Paths, line numbers and stack frames differ on every machine, and the wording is tightened between versions. The part that identifies the problem is ERR_MODULE_NOT_FOUND.

How to fix it

Cause 1 of 5

The import has no file extension.

ES modules do not guess extensions the way CommonJS does. Write it out: change import { x } from './utils' to import { x } from './utils.js'. This applies to TypeScript sources too — when the output is ESM, the specifier in your .ts file must already say .js, because that is the file that will exist at runtime.

Cause 2 of 5

The specifier points at a directory and expects an index file.

The ESM loader has no directory-index resolution. Import the file explicitly — ./utils/index.js — or give that folder a package.json with an exports field naming its entry point.

Cause 3 of 5

The package is not installed, or node_modules is stale after a branch switch.

Run npm ci to install exactly what the lockfile says, then confirm with npm ls <package> that it resolved. npm ci is the safe reinstall — it never silently changes the lockfile.

Cause 4 of 5

The case of the specifier does not match the file on disk.

macOS and Windows are case-insensitive by default; Linux containers and CI are not. ./Utils.js and ./utils.js are two different files in production. Rename the import to match the file exactly.

Cause 5 of 5

You are importing a subpath the package does not publish.

A package with an exports map in its package.json exposes only the subpaths listed there; everything else is deliberately unreachable. Open that file and import a path it actually declares.

Stopping it coming back

Keep the file extension on every relative ESM import, and run at least one CI job on Linux so a case mismatch fails in review instead of in production.

ERR_MODULE_NOT_FOUND — common questions

Why do I need .js on imports when the file is TypeScript?
Because the specifier is not rewritten during compilation. TypeScript emits the import string as you wrote it, so a runtime ESM loader receives './utils' and looks for a file with exactly that name. Writing './utils.js' in the .ts source is correct and expected under the nodenext and node16 module settings — the compiler resolves it back to utils.ts for type checking.
How is this different from Cannot find module?
ERR_MODULE_NOT_FOUND comes from the ES module loader (import statements). The plainer 'Error: Cannot find module' comes from the CommonJS loader (require). The causes overlap, but the extension and directory-index rules only bite in the ESM case.
Can I turn the CommonJS resolution behaviour back on?
Not for real code. Node has a --experimental-specifier-resolution flag in some versions, but it was never stabilised and it will not exist in the environment you deploy to. Adding the extension is a two-character change that works everywhere.

Test the fix before you paste it back

Reproducing the failure in a scratch file is usually faster than editing the real one. AltF Code is a free in-browser editor with real syntax highlighting for HTML, CSS, JavaScript, TypeScript, Python, SQL and JSON, and it runs HTML, CSS and JavaScript live in a preview panel. No install and no sign-up.

Open the editor