#how do i import main.js/index.js file into a command.js file

13 messages · Page 1 of 1 (latest)

kindred pelican

if i want to make a commands folder then how should i import the main.js file into the command js file?

open slate

Why would you want to? Sounds like you want circular imports, which is never a good idea

kindred pelican

No i dont want circular import

i mean like would

let module = require(`./main.js`)

client.on(Events.MessageCreate, (message) => {
    if (!message.content.startsWith(prf) || message.author.bot) return;
  
    const args = message.content.slice(prf.length).split(" ");
    const command = args.shift().toLowerCase();
  
    if (command === "ping") {
      message.channel.send("Pong!");
    }
  });
import { prf } from "module";
import { client } from "module";
``` work?
open slate

Again, why would you want to? The client already is available as message.client
Your code shows you actually making circular imports

kindred pelican

oh i did not know

kindred pelican
open slate

That’s even worse reason to do that

kindred pelican

why?

i mean why is circular import bad?

open slate

Look at it this way: for A to work you need to first import B, so let‘s do that. While trying to import B we see we need to first import A, so let‘s do that.
(If node was dumb this would now recurse infinitely, thankfully node is a little more intelligent so it now throws and tells you it couldn’t load B fully because it tried to load a file that was about to be loaded but didn’t fully load yet)

kindred pelican

how do i import A?

sorry for alot of questions it just that im having a struggling time to figure out discord.js/node.js