You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为什么执行node main.js命令后我的Discord Bot无法正常上线?

问题排查及解决方法

核心原因

  • 安装依赖时命令行的工作路径不在项目根目录,依赖被安装到了其他位置,当前项目下没有生成node_modules文件夹
  • package.json配置不完整,你提供的代码末尾缺少闭合的},导致npm无法正常读取配置执行安装
  • npm缓存损坏或安装时权限不足,导致依赖安装流程中断,没有生成对应文件
  • Node.js版本不符合discord.js的最低要求,导致依赖安装失败

解决步骤

  1. 先修复package.json配置,在文件末尾补充缺失的},完整配置如下:
{
  "name": "bot",
  "version": "1.0.0",
  "description": "test",
  "main": "main.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Cookie482",
  "license": "ISC",
  "dependencies": {
    "discord.js": "*"
  }
}
  1. 打开命令提示符,执行以下命令切换到项目根目录:
    cd C:\Users\Cookie\Desktop\bot
  2. 执行命令清理npm缓存:
    npm cache clean --force
  3. 重新执行依赖安装命令:
    npm install
    安装完成后检查项目根目录是否生成node_modules文件夹和package-lock.json文件,确认生成后再启动项目。
  4. 若安装后仍有报错,执行node -v查看Node.js版本,discord.js v14及以上版本要求Node.js版本≥16.9.0,版本过低请升级Node.js到最新LTS版本。
  5. 依赖问题解决后如果启动时出现Client相关报错,是因为discord.js v14+初始化需要指定Intents,修改main.js代码如下即可:
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({ 
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMessages,
    GatewayIntentBits.MessageContent
  ] 
});

client.once('ready', () => {
    console.log('bot is online');
});

client.login('替换为你自己的bot-token');

内容的提问来源于stack exchange,提问作者Cookie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 18:03:03