node:internal/modules/cjs/loader says Cannot find module for discord.js
node:internal/modules/cjs/loaderShort answer
The loader looks for discord.js in node_modules folders from your script's directory upward. It only fails when the package is not installed there, or a relative path points to a file that is not where you run from. Install it in the project folder, run from the right folder, and check your Node.js version.
Fix it fast
1Find out which cause you have
Read the first line of the error. It names the module that is missing, and the Require stack lines name the file that asked for it.
node index.js 2>&1 | head -n 122Match what you see, then jump to its fix
- 1
Cannot find module 'discord.js'discord.js is not installed in this projectFix - 2
Cannot find module './config.json' (or another path starting with ./ or ../)A relative path does not match the file locationFix - 3
Problems start after a fresh install of discord.js or on a server with an old Node.jsThe Node.js version is older than discord.js supportsFix
- 1
What does the error look like?
node:internal/modules/cjs/loaderError: Cannot find module 'discord.js'code: 'MODULE_NOT_FOUND'
Cause 1 of 3
discord.js is not installed in this project
1Check
For a bare name Node.js searches node_modules, starting at the directory of the current file and moving up one level at a time. If no folder on that path holds the package, it throws MODULE_NOT_FOUND.
ls node_modules | grep -i discord2Fix
Run the install in the folder that holds package.json. Plain npm install reads package.json; naming the package adds it to dependencies.
cd /path/to/bot
npm install discord.js3Verify
Start the bot again.
node index.jsConfirmed by docs.npmjs.com
Cause 2 of 3
A relative path does not match the file location
1Check
A path starting with ./ is relative to the file that calls require, not to the folder you ran the command in. A missing extension other than .js, .json or .node is not tried.
ls
grep -n "require(" index.js2Fix
Correct the path, or add the full extension for files such as .cjs.
const config = require('./config.json');
const handler = require('./events/ready.cjs');3Verify
Run the script again.
node index.jsConfirmed by nodejs.org
Cause 3 of 3
The Node.js version is older than discord.js supports
1Check
The discord.js README names the minimum Node.js version it needs.
node -v2Fix
Upgrade Node.js to at least the version the discord.js README states, then reinstall dependencies.
node -v
rm -rf node_modules
npm install3Verify
Start the bot.
node index.jsConfirmed by github.com
How do I stop it coming back?
- Add a start script to package.json and run npm start, so the working folder is always the project.
- Commit package.json and the lockfile; run npm install on every server before first start.
- Pin the Node.js version in the engines field and in your deploy setup.
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
ng serve says it cannot determine the project or target
Cannot determine project for command.
The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.
3 causes, each with a fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
ng serve cannot find the dev-server builder package
Could not find the implementation for builder @angular/build:dev-server
The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.
3 causes, each with a fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fixBack end
The command npm run build -- --prod exited with code 1 (Angular)
The command "npm run build -- --prod" exited with code 1
Exit code 1 only says the build failed; the real message is above it.
2 causes, each with a fix