TypeError: Cannot read property 'send' of undefined

Please explain why I am getting this error:
TypeError: Cannot read property ‘send’ of undefined

// server.js
const Discord = require(‘discord.js’);
const client = new Discord.Client();

client.on(‘ready’, () => {
const something = require(’./folder/file.js’);
setInterval(something.word, 10000);

// env.general - discord channel id
const general = client.channels.get(env.general);
if (!general) return;
module.exports = { general };
});

// file.js
function word() {
const channel = require(’…/server.js’);
channel.general.send(‘ok’);
}
module.exports = { word };

// node v 10.x

Hi @AndriyMeleshko – have you verified that general is defined in server.js (and that it’s not short-circuiting with that if (!general) return? I’d be happy to take a look at your code if the project is public

Its because of the load / compile order of the require functions.

Edit - see https://nodejs.org/api/modules.html#modules_cycles

A better way to do it is to have only file.js as a module, and pass it parameters, like …

setInterval(() => something.word(general), 10000);

Hey, can you send me an invite code to the project so I can see what server.js is cause you never really showed it. And, is there more?

Use process.env.general instead of env.general

3 Likes