Circular Dependency Issue

Hey, I'm currently experiencing a weird circular dependency issue. I have 3 typescript files, 1 main file, 1 file to import the event files and 1 event file. In the main file i'm using require to import my event files. The reason for this is because the variable client should first be populated before using it in the other files. This is my main file (Client.ts):
import 'dotenv/config';
import { ActivityType, Client as DiscordClient, Events } from 'discord.js';
import { ClientConfigOptions } from './utils/Configurations.js';
import { logger } from './utils/Logger.js';

export const client: DiscordClient = new DiscordClient(ClientConfigOptions);

client.once(Events.ClientReady, async (c): Promise<void> => {
logger.log({ level: 'info', message: `Client ready as ${c.user.tag}` });
client.user?.setPresence({
status: 'online',
afk: false,
activities: [
{
name: `${client.guilds.cache.size} servers`,
type: ActivityType.Listening,
},
],
});

require('./utils/Initializer.js');
});

client
.login(process.env.DISCORD_CLIENT_TOKEN)
.then(() => logger.log({ level: 'info', message: 'Client logged in' }))
.catch((e) => logger.log({ level: 'error', message: e }));
import 'dotenv/config';
import { ActivityType, Client as DiscordClient, Events } from 'discord.js';
import { ClientConfigOptions } from './utils/Configurations.js';
import { logger } from './utils/Logger.js';

export const client: DiscordClient = new DiscordClient(ClientConfigOptions);

client.once(Events.ClientReady, async (c): Promise<void> => {
logger.log({ level: 'info', message: `Client ready as ${c.user.tag}` });
client.user?.setPresence({
status: 'online',
afk: false,
activities: [
{
name: `${client.guilds.cache.size} servers`,
type: ActivityType.Listening,
},
],
});

require('./utils/Initializer.js');
});

client
.login(process.env.DISCORD_CLIENT_TOKEN)
.then(() => logger.log({ level: 'info', message: 'Client logged in' }))
.catch((e) => logger.log({ level: 'error', message: e }));
This is my mapping file (Initializer.ts):
import '../client/events/channel/ChannelCreate.js';
import '../client/events/channel/ChannelCreate.js';
And this is my event file (ChannelCreate.ts):
import { Events, NonThreadGuildBasedChannel } from 'discord.js';
import { client } from '../../../Client.js';

console.log('Loaded');

client.on(Events.ChannelCreate, (channel: NonThreadGuildBasedChannel) => {
console.log(channel);
});
import { Events, NonThreadGuildBasedChannel } from 'discord.js';
import { client } from '../../../Client.js';

console.log('Loaded');

client.on(Events.ChannelCreate, (channel: NonThreadGuildBasedChannel) => {
console.log(channel);
});
No description
2 Replies
d.js toolkit
d.js toolkit•13mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
Derp.
Derp.•13mo ago
This is my tsconfig.json file.
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"rootDir": "./src/",
"outDir": "./build/",
"strict": true,
"moduleResolution": "NodeNext",
"importHelpers": true,
"experimentalDecorators": true,
"esModuleInterop": true,
"skipLibCheck": true,
"allowSyntheticDefaultImports": true,
"resolveJsonModule": true,
"forceConsistentCasingInFileNames": true,
"removeComments": true,
"typeRoots": [
"node_modules/@types"
],
"sourceMap": false,
"baseUrl": "./"
},
"include": [
"./**/*.ts"
],
"exclude": [
"node_modules",
"build"
],
}
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"rootDir": "./src/",
"outDir": "./build/",
"strict": true,
"moduleResolution": "NodeNext",
"importHelpers": true,
"experimentalDecorators": true,
"esModuleInterop": true,
"skipLibCheck": true,
"allowSyntheticDefaultImports": true,
"resolveJsonModule": true,
"forceConsistentCasingInFileNames": true,
"removeComments": true,
"typeRoots": [
"node_modules/@types"
],
"sourceMap": false,
"baseUrl": "./"
},
"include": [
"./**/*.ts"
],
"exclude": [
"node_modules",
"build"
],
}
Oh i see, i will try that. Thank you for your fast response 🙂
Want results from more Discord servers?
Add your server