导入不和谐机器人模块时出现参考错误

Reference errors appearing when importing discord bot modules

好吧,我正在写一个音乐机器人,因为更多的流行音乐被关闭了,剩下的几乎没用了。为了更好的可读性和所有这些,我将代码拆分在单独的文件中,因此我使用模块来导入和导出函数和变量,当我 运行 使用 node index.js 的程序时,它告诉我我正在一个引用错误,我需要在使用前初始化一个变量,即使它已经被初始化了。

这是我的 index.js 代码:

//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" })
});

这是我的 play.js 代码:

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){
}

最后,这是我得到的错误:

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)

我试图在网上找到有同样问题的人,但是 none 他们中有同样的问题。我发现的是,当我从 play.js 导入 getCommand 函数时,它会中断,我已经尝试 运行 分别处理这两个文件,如果函数是,它们仍然会中断导入 index.js。我会从 index.js 中删除函数导入,但我不能,因为这是我计划在 play.js 文件的其余部分中调用其余函数的方式。

根据@ouroborus 的评论,这是循环依赖问题。我通过创建一个新文件名 global-vars 解决了这个问题,我在其中放置了它们两个所需的所有全局变量,以便它们依赖于第三个文件而不是彼此依赖,它还允许稍微更简洁的代码。