Maaz Zindani

Loading the index…

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

node:internal/modules/cjs/loader says Cannot find module for discord.js

Errornode:internal/modules/cjs/loader

Short 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.

  • Applies to Node.js 22, Node.js 24, discord.js 14
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 12
  2. 2Match what you see, then jump to its fix

    1. 1Cannot find module 'discord.js'discord.js is not installed in this projectFix
    2. 2Cannot find module './config.json' (or another path starting with ./ or ../)A relative path does not match the file locationFix
    3. 3Problems 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

What does the error look like?

  • node:internal/modules/cjs/loader
  • Error: 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 discord

2Fix

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.js

3Verify

Start the bot again.

node index.js

Confirmed by docs.npmjs.com

Did this fix it?

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.js

2Fix

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.js

Confirmed by nodejs.org

Did this fix it?

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 -v

2Fix

Upgrade Node.js to at least the version the discord.js README states, then reinstall dependencies.

node -v
rm -rf node_modules
npm install

3Verify

Start the bot.

node index.js

Confirmed by github.com

Did this fix it?

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 fix

Back 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 fix

Next step

Want this handled for good?

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