Docker中运行discord.js机器人报错:ReferenceError: ReadableStream未定义
问题:Discord.js机器人在Docker容器中运行报错ReferenceError: ReadableStream is not defined
我有一个discord.js机器人,本地运行一切正常,但在GitLab流水线的Docker容器中运行时出现ReferenceError: ReadableStream is not defined错误。
机器人核心逻辑代码
import {Client, GatewayIntentBits, ThreadChannel} from "discord.js"; import {startCommandBuilder} from "./builders/slashCommandBuilders"; import Database from "./classes/databaseClass"; import BotInteraction from "./classes/interactionClass"; import mainEvents from "./events/mainEvents"; import messageEvent from "./events/messageEvent"; export const client = new Client({intents: [ GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildModeration, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildBans, GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessagePolls, GatewayIntentBits.GuildMessages ]}) const env = process.env export const mysqlConfig = { user: 'bot', password: env.MYSQL_PASSWORD, database: env.MYSQL_DATABASE, host: env.MYSQL_HOST, port: env.MYSQL_PORT } client.on('ready', async (event) => { if (!event.user) return event.user.setStatus("dnd") await event.application.commands.create(startCommandBuilder) const db = new Database() await db.init() console.log(`${event.user.displayName} started!`) }) client.on('interactionCreate', async (event) => { const interaction = new BotInteraction(event) await mainEvents(interaction) }) client.on('messageCreate', async (message) => { if (!message.member || message.member.user.bot || !(message.channel instanceof ThreadChannel)) return await messageEvent(message) }) client.login(env.BOT_TOKEN)
容器中出现的错误日志
ReferenceError: ReadableStream is not defined at Object.<anonymous> (/bot/node_modules/@discordjs/rest/node_modules/undici/lib/web/fetch/response.js:530:3) at Module._compile (node:internal/modules/cjs/loader:1198:14) at Module._extensions..js (node:internal/modules/cjs/loader:1252:10) at Object.require.extensions.<computed> [as .js] (/bot/node_modules/ts-node/src/index.ts:1608:43) at Module.load (node:internal/modules/cjs/loader:1076:32) at Function.Module._load (node:internal/modules/cjs/loader:911:12) at Module.require (node:internal/modules/cjs/loader:1100:19) at require (node:internal/modules/cjs/helpers:119:18) at Object.<anonymous> (/bot/node_modules/@discordjs/rest/node_modules/undici/lib/web/fetch/index.js:11:5) at Module._compile (node:internal/modules/cjs/loader:1198:14) /bot/node_modules/@discordjs/rest/node_modules/undici/lib/web/fetch/response.js:530 ReadableStream ^
原因分析及解决办法
核心原因
- Node.js版本不兼容:
ReadableStream是Node.js 18.x及以上版本才全局内置的API,你的Docker容器使用的Node.js版本低于18.x,而本地环境版本符合要求,导致本地正常运行、容器报错。discord.js v14+依赖的undici库(用于处理HTTP请求)明确需要Node.js 18+的环境支持。 - Docker镜像版本过时:如果Dockerfile中使用的是旧版Node镜像(如
node:16-alpine),就会触发该错误。
解决步骤
- 修改Dockerfile中的Node镜像为18.x或更高版本,例如:
FROM node:18-alpine # 后续构建步骤... - 在项目的
package.json中添加engines字段,明确Node.js版本要求:"engines": { "node": ">=18.0.0" } - 重新构建Docker镜像并在GitLab流水线中部署运行
内容的提问来源于stack exchange,提问作者Vanchugov Artemy
相关产品推荐
相关产品推荐

