运行机器人时出现TypeError: Cannot read properties of undefined (reading 'Guilds')错误求助
解决 GatewayIntentBits.Guilds 未定义的 TypeError 错误
检查Discord.js版本兼容性
这个错误大概率是版本不匹配导致的。GatewayIntentBits是Discord.js v14及以上版本才有的导出项,若代码基于v14+编写但安装的是旧版本(如v13及以下),就会触发该错误。
操作步骤:- 查看当前Discord.js版本:
npm list discord.js - 若版本低于v14,升级至最新稳定版:
npm install discord.js@latest
- 查看当前Discord.js版本:
确认导入语句正确性
即便安装了正确版本,错误的导入方式也会导致GatewayIntentBits未被正确加载。
ES模块正确写法:import { GatewayIntentBits } from 'discord.js';CommonJS正确写法:
const { GatewayIntentBits } = require('discord.js');注意避免拼写错误,比如误写
GatewayIntentBits的名称。排查依赖冲突问题
项目中其他依赖包若与Discord.js版本冲突,可能导致模块导出异常。
解决方法:- 删除
node_modules文件夹和package-lock.json(或yarn.lock) - 重新安装所有依赖:
npm install
- 删除
检查变量是否被误覆盖
排查代码中是否在导入后重新给GatewayIntentBits赋值,例如:const { GatewayIntentBits } = require('discord.js'); // 错误操作:覆盖了原有变量 GatewayIntentBits = {};这类操作会清空原变量,导致访问
Guilds时报错。
内容的提问来源于stack exchange,提问作者Summerlytx
相关产品推荐
相关产品推荐

