Cannot set property 'commands' of undefined

I was working on my new discord bot and using to set up my command manager and when I run the bot I get an error of

client.commands = new Discord.Collection();

TypeError: Cannot set property 'commands' of undefined

Everything is defined,

const { Client, MessageEmbed, Activity, RoleManager, Message, member, GuildChannel, Guild,} = require('discord.js');
const botinfo = require("./package.json");
const fs = require('fs');
const Discord = require('discord.js');
const { prefix, token } = require('./config.json');
const {client, bot} = new Discord.Client();
client.commands = new Discord.Collection();

So I have no idea what’s wrong, or if it’s me or the code
Thanks for any help you can give - Gatorz

Not sure about this syntax, never seen variables declared like that in JS :slight_smile:

Please could you try just:

const client = new Discord.Client();

Its like that because my old code uses bot, backwards compaaiblty

Just a quick reply to confirm what SteGriff has posted. This isn’t a Glitch or Discord bot thing but rather pure JavaScript. If the error tells you a reference is undefined then (by definition) it is not defined. We can think so or try to insist but the JS interpreter pretty much knows.

I’m not arguing with you but there is no “backwards compatibility” excuse if the syntax is wrong. If you write it correctly it runs, if you don’t it doesn’t.

Just for example, if you open dev tools and try

const {x, y} = new Date()

Then try the values of x and y, they’ll both be undefined:


I’d recommend that since the whole codebase is yours, and you can do whatever you want to it, just find+replace ‘bot’ with ‘client’ :slight_smile:

Or another approach: if you want bot and client to be pointers to the same client, use

const client = new Discord.Client();
const bot = client;

…and if you want them to be separate instances:

const client = new Discord.Client();
const bot = new Discord.Client();

Please try it and I think you’ll see that it fixes your error.

It’s the destructuring pattern.

function openLunchbox() {
  return {
    sandwich: "peanut butter",
    fruit: [
    drink: "iced coffee"

const { drink } = openLunchbox();

It’s likely that the OP is misusing it in this case.

1 Like

Yes, in your example, openLunchbox returns something which can be destructured.

Does the new Client() call of discord.js return an object with two members, bot and client, which are available to assign into the target object?

I’m guessing it is a mistake, but I understand where the mistake came from.

Thanks for the instructive example though :blush: