启动Discord机器人时遭遇MODULE_NOT_FOUND错误求助(已安装discord.js)
MODULE_NOT_FOUND Error for Your Discord Bot Hey there, let's work through this error you're facing. That MODULE_NOT_FOUND message pointing to your index.js usually means Node can't locate the discord.js module you thought you installed. Here are the step-by-step fixes I recommend trying:
Double-check you installed dependencies in the right folder
It's super easy to accidentally runnpm installfrom your Desktop instead of the actual bot project folder. Open your terminal/command prompt and run:cd C:\Users\Me\Desktop\Discord Bot npm install discord.jsThis ensures the module gets installed directly in your bot's directory where Node can find it.
Verify your import statement is correct
Make sure the line in yourindex.jsthat imports discord.js matches the version you're using. For discord.js v14+ (the current stable version), it should look like this:const { Client, GatewayIntentBits } = require('discord.js');If you're using an older version (v13 or below), it might be:
const Discord = require('discord.js');Typos here (like
discordjsinstead ofdiscord.js) will also trigger this error, so double-check the spelling!Clear npm cache and reinstall dependencies
Sometimes npm's cache gets corrupted, leading to missing modules. Try these steps:- Delete the
node_modulesfolder in your bot directory - Delete the
package-lock.jsonfile (don't worry, this will get regenerated) - Run these commands in your terminal:
npm cache clean --force npm install
- Delete the
Check your Node.js version
Discord.js v14 requires Node.js 16.9.0 or newer. Run this command to check your current version:node -vIf your version is lower than 16.9.0, upgrade to the latest LTS version—this will fix compatibility issues that might be causing the module to not load.
Confirm discord.js is listed in your package.json
Open thepackage.jsonfile in your bot folder and look for thedependenciessection. You should see an entry like:"dependencies": { "discord.js": "^14.11.0" }If it's not there, that means the initial install didn't complete successfully—rerun
npm install discord.jsto add it.
Try these steps one by one, and your bot should start up without that error!
内容的提问来源于stack exchange,提问作者Keppy

