JS Running Error - Cannot read properties of undefined

Hey! I am making my bot but I feel like I have had this error before but I don’t know if I made a post but yeah. Error is below: (javascript)

PS C:\Users\crazy\OneDrive\Desktop\mystical-bot-real> node . 
C:\Users\crazy\OneDrive\Desktop\mystical-bot-real\index.js:5
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.DIRECT_MESSAGES] });
                                              ^

TypeError: Cannot read properties of undefined (reading 'FLAGS')
    at Object.<anonymous> (C:\Users\crazy\OneDrive\Desktop\mystical-bot-real\index.js:5:47)
    at Module._compile (node:internal/modules/cjs/loader:1099:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47

Node.js v17.7.1
PS C:\Users\crazy\OneDrive\Desktop\mystical-bot-real>

If this is something to do with my index.js file please tell me and I will reply to your post with the code!

And yes, I have enabled all Privileged Gateway Intents.

Are your dependencies up to date?

Once again, I barely understand JS.

Like npm, discord.js and fs?

Correct, npm update dicsord.js

Updated but still

PS C:\Users\crazy\OneDrive\Desktop\mystical-bot-real> node .
C:\Users\crazy\OneDrive\Desktop\mystical-bot-real\index.js:5
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.DIRECT_MESSAGES] });
                                              ^

TypeError: Cannot read properties of undefined (reading 'FLAGS')
    at Object.<anonymous> (C:\Users\crazy\OneDrive\Desktop\mystical-bot-real\index.js:5:47)
    at Module._compile (node:internal/modules/cjs/loader:1099:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47

Node.js v17.7.1
PS C:\Users\crazy\OneDrive\Desktop\mystical-bot-real>

Also, are you importing the intents? I think you need to do that with discord.js.

E.g:

const { Client, GatewayIntentBits } = require('discord.js');

const client = new Client({ intents: [GatewayIntentBits.Guilds] });
Another example I found
const { Client, IntentsBitField } = require('discord.js');

const myIntents = new IntentsBitField();
myIntents.add(IntentsBitField.Flags.GuildPresences, IntentsBitField.Flags.GuildMembers);

const client = new Client({ intents: myIntents });

// other examples:
const otherIntents = new IntentsBitField([IntentsBitField.Flags.Guilds, IntentsBitField.Flags.DirectMessages]);
otherIntents.remove([IntentsBitField.Flags.DirectMessages]);

const otherIntents2 = new IntentsBitField(32509);
otherIntents2.remove(4096, 512);

What even is the intent Intents.FLAGS.GUILDS, I’ve never heard of that. Maybe this is what you’re intending of achieving:

const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES"] })
const fs = require('fs');
const { Client, Collection, Intents } = require('discord.js');
const { token } = require('./config.json');

const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.DIRECT_MESSAGES] });

client.commands = new Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
	const command = require(`./commands/${file}`);
	client.commands.set(command.data.name, command);
}

const eventFiles = fs.readdirSync('./events').filter(file => file.endsWith('.js'));

for (const file of eventFiles) {
	const event = require(`./events/${file}`);
	if (event.once) {
		client.once(event.name, (...args) => event.execute(...args));
	} else {
		client.on(event.name, (...args) => event.execute(...args));
	}
}

client.login(token);*emphasized text*

This is my index.js file btw and I dont think its the problem?

Try the following:

Once again, JavaScript is :alien: to me.

1 Like

Added it in and I got this error.

C:\Users\crazy\OneDrive\Desktop\mystical-bot-real\index.js:5
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.DIRECT_MESSAGES] });
    at Object.<anonymous> (C:\Users\crazy\OneDrive\Desktop\mystical-bot-real\index.js:5:47)
    at Module._compile (node:internal/modules/cjs/loader:1099:14)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47

Node.js v17.7.1
PS C:\Users\crazy\OneDrive\Desktop\mystical-bot-real> node .
C:\Users\crazy\OneDrive\Desktop\mystical-bot-real\index.js:5
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.DIRECT_MESSAGES] });
                                              ^

TypeError: Cannot read properties of undefined (reading 'FLAGS')
    at Object.<anonymous> (C:\Users\crazy\OneDrive\Desktop\mystical-bot-real\index.js:5:47)
    at Module._compile (node:internal/modules/cjs/loader:1099:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47

Node.js v17.7.1
PS C:\Users\crazy\OneDrive\Desktop\mystical-bot-real> 

You have two clients, remove the old one.

Also remove the Discord from Discord.Client.

Error:

PS C:\Users\crazy\OneDrive\Desktop\mystical-bot-real> node .
C:\Users\crazy\OneDrive\Desktop\mystical-bot-real\index.js:5
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES"] })
               ^

ReferenceError: Discord is not defined
    at Object.<anonymous> (C:\Users\crazy\OneDrive\Desktop\mystical-bot-real\index.js:5:16)
    at Module._compile (node:internal/modules/cjs/loader:1099:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47

Node.js v17.7.1
PS C:\Users\crazy\OneDrive\Desktop\mystical-bot-real>

Code:

const fs = require('fs');
const { Client, Collection, Intents } = require('discord.js');
const { token } = require('./config.json');

const client = new Client({ intents: ["GUILDS", "GUILD_MESSAGES"] })

client.commands = new Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
	const command = require(`./commands/${file}`);
	client.commands.set(command.data.name, command);
}

const eventFiles = fs.readdirSync('./events').filter(file => file.endsWith('.js'));

for (const file of eventFiles) {
	const event = require(`./events/${file}`);
	if (event.once) {
		client.once(event.name, (...args) => event.execute(...args));
	} else {
		client.on(event.name, (...args) => event.execute(...args));
	}
}

client.login(token);

As stated above, please do the following.

1 Like

I have never seen this error in my life…

PS C:\Users\crazy\OneDrive\Desktop\mystical-bot-real> node .
C:\Users\crazy\OneDrive\Desktop\mystical-bot-real\node_modules\discord.js\src\util\BitField.js:168
    throw new RangeError(ErrorCodes.BitFieldInvalid, bit);
    ^

RangeError [BitFieldInvalid]: Invalid bitfield flag or number: GUILDS.
    at Function.resolve (C:\Users\crazy\OneDrive\Desktop\mystical-bot-real\node_modules\discord.js\src\util\BitField.js:168:11)
    at C:\Users\crazy\OneDrive\Desktop\mystical-bot-real\node_modules\discord.js\src\util\BitField.js:163:54
    at Array.map (<anonymous>)
    at Function.resolve (C:\Users\crazy\OneDrive\Desktop\mystical-bot-real\node_modules\discord.js\src\util\BitField.js:163:40)
    at Client._validateOptions (C:\Users\crazy\OneDrive\Desktop\mystical-bot-real\node_modules\discord.js\src\client\Client.js:482:41)
    at new Client (C:\Users\crazy\OneDrive\Desktop\mystical-bot-real\node_modules\discord.js\src\client\Client.js:78:10)
    at Object.<anonymous> (C:\Users\crazy\OneDrive\Desktop\mystical-bot-real\index.js:5:16)
    at Module._compile (node:internal/modules/cjs/loader:1099:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:975:32) {
  code: 'BitFieldInvalid'
}

Node.js v17.7.1
PS C:\Users\crazy\OneDrive\Desktop\mystical-bot-real> 

Out of curiosity, what are the intents you need?

In python we do the following:

intents = discord.Intents(messages = True, guilds = True, reactions = True, members = True, presences = True)
bot = commands.Bot(command_prefix = '#', intents = intents)

After some more scanning of the discord.js tags, I’ve come over this:

const { Client, GatewayIntentBits } = require('discord.js');

const client = new Client({ intents: [GatewayIntentBits.Guilds] });

In your case you can try:

const { Client, GatewayIntentBits } = require('discord.js');

const client = new Client({ intents: [GatewayIntentBits.Guilds]})

Do I have to replace any of my code?

const fs = require('fs');
const { Client, Collection, Intents } = require('discord.js');
const { token } = require('./config.json');

const client = new Client({ intents: ["GUILDS", "GUILD_MESSAGES"] })

client.commands = new Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
	const command = require(`./commands/${file}`);
	client.commands.set(command.data.name, command);
}

const eventFiles = fs.readdirSync('./events').filter(file => file.endsWith('.js'));

for (const file of eventFiles) {
	const event = require(`./events/${file}`);
	if (event.once) {
		client.once(event.name, (...args) => event.execute(...args));
	} else {
		client.on(event.name, (...args) => event.execute(...args));
	}
}

client.login(token);

Don’t directly copy and paste this; read it first! :wink:

1 Like
PS C:\Users\crazy\OneDrive\Desktop\mystical-bot-real> node .
C:\Users\crazy\OneDrive\Desktop\mystical-bot-real\node_modules\discord.js\src\util\BitField.js:168
    throw new RangeError(ErrorCodes.BitFieldInvalid, bit);
    ^

RangeError [BitFieldInvalid]: Invalid bitfield flag or number: GUILDS.
    at Function.resolve (C:\Users\crazy\OneDrive\Desktop\mystical-bot-real\node_modules\discord.js\src\util\BitField.js:168:11)
    at C:\Users\crazy\OneDrive\Desktop\mystical-bot-real\node_modules\discord.js\src\util\BitField.js:163:54
    at Array.map (<anonymous>)
    at Function.resolve (C:\Users\crazy\OneDrive\Desktop\mystical-bot-real\node_modules\discord.js\src\util\BitField.js:163:40)
    at Client._validateOptions (C:\Users\crazy\OneDrive\Desktop\mystical-bot-real\node_modules\discord.js\src\client\Client.js:482:41)
    at new Client (C:\Users\crazy\OneDrive\Desktop\mystical-bot-real\node_modules\discord.js\src\client\Client.js:78:10)
    at Object.<anonymous> (C:\Users\crazy\OneDrive\Desktop\mystical-bot-real\index.js:5:16)
    at Module._compile (node:internal/modules/cjs/loader:1099:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:975:32) {
  code: 'BitFieldInvalid'
}

Node.js v17.7.1
PS C:\Users\crazy\OneDrive\Desktop\mystical-bot-real> 

Another error.

Can I see your code?