Node.js crashes in internal/modules/cjs/loader with MODULE_NOT_FOUND
node:internal/modules/cjs/loaderShort 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.
Fix it fast
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'))" ls2Match what you see, then jump to its fix
- 1
The 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
The missing name is a package such as express, with no ./ in front, and node_modules is missing or oldA dependency is not installedFix - 3
The missing name starts with ./ or ../A relative require points at the wrong fileFix
- 1
What does the error look like?
node:internal/modules/cjs/loaderthrow 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.js2Fix
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.js3Verify
The app starts without the loader error.
node app.jsConfirmed by nodejs.org
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 express2Fix
Install what package.json lists, or add the package if it was never saved.
npm install
npm install express3Verify
The package now resolves from your project folder.
node -e "console.log(require.resolve('express'))"Confirmed by nodejs.org
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 lib2Fix
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
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
Back end
Application Load Balancer returns 502 for a Node.js target
HTTPCode_ELB_502_Count
An ALB returns 502 when the target gives it something it cannot use: a refused or reset connection, a connection closed mid-request, a bad response, or a header over 32 K.
4 causes, each with a fixBack end
corepack enable fails: permission denied while creating the pnpm link
Internal Error: EACCES: permission denied, symlink '../lib/node_modules/corepack/dist/pnpm.js' -> '/usr/local/bin/pnpm'
corepack enable writes small link files for yarn and pnpm next to your node binary.
2 causes, each with a fixBack end
Create React App says Node is too old, and npm will not run on the new one
npm does not support Node.js v10.19.0
The shell is running an old Node.js that is below the version Create React App needs.
3 causes, each with a fixBack end
firebase deploy cannot load your functions: user code failed to load
Functions codebase could not be analyzed successfully. It may have a syntax or runtime error
The Firebase CLI loads your functions code on your machine before it deploys, to learn which functions exist.
4 causes, each with a fixBack end
firebase deploy stops because the predeploy lint step failed
Error: There was an error deploying functions
The predeploy hook in firebase.json runs npm run lint before every deploy, and a failing hook cancels the deploy.
3 causes, each with a fixBack end
fluent-ffmpeg cannot find the ffmpeg or ffprobe binary
Error: Cannot find ffprobe
fluent-ffmpeg does not bundle ffmpeg.
4 causes, each with a fix