Ok so I am writing a music bot due to the more populars getting shutdown and the remaining being pretty useless. I split up the code in separate files for better readability and all that and so am using modules to import and export function & variables and when I run the program using node index.js
it tells me that I’m getting a reference error and that I need to initialized a variable before use even though it’s already been initialized.
This is my index.js
code:
//Dependency imports import { Client, Intents } from 'discord.js'; import ytmp3 from 'youtube-mp3-converter' import puppeteer from 'puppeteer' import fse from 'fs-extra'; import {} from 'dotenv/config' //Import global variables import playCommand from './src/play.js' //Exported global variables export const token = process.env.TOKEN; export const client = new Client({ intents:[Intents.FLAGS.DIRECT_MESSAGES, Intents.FLAGS.GUILD_VOICE_STATES, Intents.FLAGS.DIRECT_MESSAGE_REACTIONS, Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MESSAGE_REACTIONS, Intents.FLAGS.DIRECT_MESSAGE_REACTIONS] }); export const globalCommands = { play: "!p" } client.login(token) client.on('ready', () => { console.log(`Logged on using ${client.user.tag}`) client.user.setActivity("Crying in JS", { type:"PLAYING" }) });
and this is my play.js
code:
import { token, client } from '../index.js' //Dependency imports import { Client, Intents } from 'discord.js'; import { createAudioPlayer } from '@discordjs/voice'; import ytmp3 from 'youtube-mp3-converter'; import fse from 'fs-extra'; import path from 'path'; const __dirname = path.resolve(); import puppeteer from 'puppeteer'; import {} from 'dotenv/config' client.login(token) //Declaration of global variables let musicFiles = fse.readdirSync(path.join(__dirname, 'musicSaves')) //Takes in message content, downloads song, adds to queueArray export default function getCommand(message){ } //declare local function below here //Tries to find link in message using Regex function getLink(messageLink, message){ const urlRegex = /https?://(www.)?[-a-zA-Z0-9@:%._+~#=]{1,256}.[a-zA-Z0-9()]{1,6}b([-a-zA-Z0-9()@:%_+.~#?&//=]*)/; let link = messageLink.match(urlRegex); link == null ? convertToMp3(link) : findSongName(message.content); } //Converts source to mp3 to be played by audio player async function convertToMp3(inputLink){ const convertLinkToMp3 = youtubeMp3Converter('../musicSaves') const pathToMp3 = await convertLinkToMp3(inputLink) } //If link isn't found; tries to find source using puppeteer async function findName(message){ }
Lastly, this is the error I get:
ReferenceError: Cannot access 'client' before initialization at file:///C:/Users/alexa/Desktop/Fortnite-Wrap-Bot--v2/src/play.js:15:1 at ModuleJob.run (node:internal/modules/esm/module_job:185:25) at async Promise.all (index 0) at async ESMLoader.import (node:internal/modules/esm/loader:281:24) at async loadESM (node:internal/process/esm_loader:88:5) at async handleMainPromise (node:internal/modules/run_main:65:12)
I’ve tried to find someone with the same problem online but none of them have had the issue in the same context. What I’ve figured out is that when I import the getCommand
function from play.js
that’s when it breaks, I’ve tried running both files seperately and they still break if the function is imported into index.js
. I would remove the function import from index.js
but I can’t since it’s how I plan to call the rest of the functions in the rest of the play.js
file.
Advertisement
Answer
From what @ouroborus commented this was circular dependency issue. I solved this by creating a new file name global-vars
in which I placed all global variables needed by both of them so that they depend on a third file and not eachother, it also allowed for slightly cleaner code.