When I run node . I keep getting the error “ReferenceError: Cannot access ‘client’ before initialization”
my script is…
const { Client, Intents } = require(“discord.js”)
const client = new Client({
Intents:[
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MEMBERS,
Intents.FLAGS.GUILD_MESSAGES
]
})
const config = require(“./config.js”)
client.login(config.TOKEN)
client.on(“ready”, async () => {
console.log(“Im Login!”)
client.user.setactivity(“Hello World!”)
})
Alright, so I went ahead and updated your code to the latest version of discord.js (v14).
There were a few things I had to change or swap-out, I will show you the old code that you had, and will provide the new code that you need to use.
If you’re using a different version of discord, Its not a problem but I do recommend updating to v14.
Old Code
const { Client, Intents } = require("discord.js")
const client = new Client({
Intents:[
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MEMBERS,
Intents.FLAGS.GUILD_MESSAGES
]
})
const config = require("./config.js")
client.login(config.TOKEN)
client.on("ready", async () => {
console.log("Im Login!")
client.user.setactivity("Hello World!")
})
Updated Code
const { Client, GatewayIntentBits } = require("discord.js")
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildMessages] });
const config = require("./config.js")
client.on("ready", async () => {
client.user.setActivity("Hello World!");
console.log("Im Login!")
});
client.login(config.token);
If you have any questions, or any issues with the updated code, please don’t hesitate to let me know!
Happy Coding,
Roman Beard
1 Like
wh0
4
did you find out what line was causing that specific error in the original? it looked like client
gets initialized pretty early
Hi! Please don’t bump old posts 
system
Closed
6
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.