ERR_MODULE_NOT_FOUND
Node.jsERRORCommonModule SystemHIGH confidence

A module could not be found.

Production Risk

Low. This is typically a startup error that prevents the application from running at all, so it is almost always caught during development or deployment.

What this means

This error occurs when a `require()` or `import` statement tries to load a module that Node.js cannot locate. This could be a core module with a typo, a third-party package that is not installed, or a local file with an incorrect path. The error message usually provides context on where Node.js looked for the module.

Why it happens
  1. 1A third-party module was imported without being installed in `node_modules` (missing `npm install`).
  2. 2The path to a local module is incorrect (e.g., `./module` vs `../module`).
  3. 3A typo exists in the module's name (e.g., `require('exress')` instead of `require('express')`).
How to reproduce

This error is thrown during the module loading phase when the `require` or `import` keyword is executed and the resolver fails to find the specified file or directory.

trigger — this will error
trigger — this will error
// Assuming 'non-existent-package' is not installed.
try {
  const myPackage = require('non-existent-package');
} catch (err) {
  console.error(err.message);
}

expected output

Error: Cannot find module 'non-existent-package'

Fix 1

Install Missing Package

WHEN The missing module is a third-party package from the npm registry.

Install Missing Package
npm install express

Why this works

Use a package manager like npm or yarn to install the dependency. This will download the package and place it in the `node_modules` directory where Node.js can find it.

Fix 2

Correct the Module Path

WHEN The missing module is a local file within your project.

Correct the Module Path
// Correct path to a local module in a parent directory.
const myModule = require('../utils/helpers.js');

Why this works

Double-check the relative path to the file. Remember that `./` refers to the current directory, `../` to the parent directory, and paths are relative to the file doing the importing.

Code examples
Triggerjs
// Assuming 'non-existent-package' is not installed.
try {
  const myPackage = require('non-existent-package');
} catch (err) {
  console.error(err.message);
}  // this triggers ERR_MODULE_NOT_FOUND
Handle in try/catchjs
try {
  // operation that may throw ERR_MODULE_NOT_FOUND
  riskyOperation()
} catch (err) {
  if (err.code === 'ERR_MODULE_NOT_FOUND') {
    console.error('ERR_MODULE_NOT_FOUND:', err.message)
  } else {
    throw err
  }
}
Defensive pattern to avoid itjs
// Use dynamic import with a fallback
const mod = await import('optional-pkg').catch(() => null)
if (!mod) console.log('Package not available')
What not to do

Sources
Official documentation ↗

https://github.com/nodejs/node/blob/main/lib/internal/modules/cjs/loader.js

More information

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Node.js errors