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

Heroku部署Discord Bot报错:无法找到模块'./discord.js'

部署Discord Bot到Heroku时遇到模块找不到错误:Error: cannot find module './discord.js'

问题描述

首次在Heroku部署Discord Bot,遇到阻碍部署的错误:Error: cannot find module './discord.js'。按教程操作但部署失败,已卡近一小时,尝试过相关解决方案但未生效。

package.json内容

{
    "name": "booster-bot",
    "version": "1.0.0",
    "main": "index.js",
    "engines": {
        "node": "12.14.1",
        "npm": "6.13.4"
    },
    "repository": {
        "type": "git",
        "url": "git+link"
    },
    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1",
        "start": "node bot.js",
        "devStart": "nodemon bot.js"
    },
    "keywords": [],
    "author": "",
    "license": "ISC",
    "description": "",
    "dependencies": {
        "dotenv": "^16.4.5",
        "requirements-txt": "^0.0.5"
    },
    "devDependencies": {
        "nodemon": "^3.1.4"
    }
}

报错指向的代码块

const {
  Client,
  Events,
  GatewayIntentBits,
  EmbedBuilder,
} = require("./discord.js");

const client = new Client({
  partials: ["GUILD_MEMBER"],
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMessages,
    GatewayIntentBits.MessageContent,
    GatewayIntentBits.GuildMembers,
    GatewayIntentBits.GuildPresences,
  ],
});

补充说明

最初的require语句没有./,后来参考相关回答添加了,但仍无效。已启用Procfile并安装了requirements.txt,相关信息可在package.json片段中看到:

{
    "name": "booster-bot",
    "version": "1.0.0",
    "lockfileVersion": 3,
    "requires": true,
    "packages": {
        "": {
            "name": "booster-bot",
            "version": "1.0.0",
            "license": "ISC",
            "dependencies": {
                "dotenv": "^16.4.5",
                "requirements-txt": "^0.0.5"
            },
            "devDependencies": {
                "nodemon": "^3.1.4"
            },
            "engines": {
                "node": "12.14.1",
                "npm": "6.13.4"
            }
        }
    }
}

解决思路

  • 安装discord.js核心依赖:你的package.json中没有包含discord.js,这是根本问题。运行npm install discord.js,确保discord.js被添加到dependencies字段中。
  • 修正require路径:discord.js是通过npm安装的第三方包,不是本地文件,所以应该用require("discord.js")而非require("./discord.js"),后者会尝试加载当前目录下的discord.js文件,而你本地不存在这个文件。
  • 升级Node.js版本:当前配置的Node.js版本12.14.1过于老旧,discord.js v14及以上版本要求Node.js 16.9.0或更高。修改package.json中engines字段为"node": "18.x"(或更高兼容版本),同时本地开发环境也要使用对应版本测试。
  • 调整Procfile配置:Discord Bot属于后台进程,不需要监听HTTP端口,因此Procfile应改为worker: node bot.js,避免Heroku因检测不到端口而终止进程。
  • 移除无关依赖:requirements-txt是用于处理Python项目依赖的包,Node.js项目不需要它,运行npm uninstall requirements-txt移除该包,减少不必要的干扰。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 09:35:54