Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Node.js crashes in internal/modules/cjs/loader with MODULE_NOT_FOUND

Errornode:internal/modules/cjs/loader

Short answer

The loader could not resolve a file or package. Read the first lines of the error: they name what could not be resolved. Three causes cover almost every case: the entry script path is wrong for the folder you ran node from, a dependency is not installed, or a relative require points at the wrong file.

  • Applies to Node.js 22, Node.js 24, npm 10, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Ask Node which file it fails to find, and where it searches for a bare package name.

    node -e "console.log(require.resolve.paths('express'))"
    ls
  2. 2Match what you see, then jump to its fix

    1. 1The path in the error is your own entry script, such as /home/me/app.jsThe entry script path is wrong for the current folderFix
    2. 2The missing name is a package such as express, with no ./ in front, and node_modules is missing or oldA dependency is not installedFix
    3. 3The missing name starts with ./ or ../A relative require points at the wrong fileFix

What does the error look like?

  • node:internal/modules/cjs/loader
  • throw err;
  • code: 'MODULE_NOT_FOUND'

Cause 1 of 3

The entry script path is wrong for the current folder

1Check

The shell resolves the script you pass to node against the current folder, so the same command works in one folder and fails in another.

pwd
ls app.js

2Fix

Run node from the project folder, or pass the full path. Do the same in package.json scripts and process manager configs.

cd /path/to/project
node app.js
# or from anywhere
node /path/to/project/app.js

3Verify

The app starts without the loader error.

node app.js

Confirmed by nodejs.org

Did this fix it?

Cause 2 of 3

A dependency is not installed

1Check

A bare name is looked up in node_modules, starting in the folder of the file that calls require and moving up one level at a time.

ls node_modules/express
npm ls express

2Fix

Install what package.json lists, or add the package if it was never saved.

npm install
npm install express

3Verify

The package now resolves from your project folder.

node -e "console.log(require.resolve('express'))"

Confirmed by nodejs.org

Did this fix it?

Cause 3 of 3

A relative require points at the wrong file

1Check

A relative path is resolved against the file that contains the require, not against the folder you ran node from. Node tries the exact name, then .js, .json and .node, then a folder's package.json main or index.js. Other extensions such as .cjs must be written out.

node -e "console.log(require.resolve('./lib/db'))"
ls lib

2Fix

Correct the path, or write the full file name including the extension. On Linux, file names are case sensitive, so check capital letters.

// app.js
const db = require('./lib/db.cjs');

3Verify

require.resolve prints the full path of the file.

node -e "console.log(require.resolve('./lib/db.cjs'))"

Confirmed by nodejs.org

Did this fix it?

How do I stop it coming back?

  • Start the app only through package.json scripts so the folder is always the same.
  • Commit package.json and the lockfile, and use npm ci in deploys.
  • Build file paths for fs calls from __dirname instead of the current folder.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.