Why is this not a function? Importing ./src/index.js
in ./index.js
works just fine, but trying to import it in ./src/Client.js
just doesn't let me. It's not some "invalid file path" because I don't have any errors, and it autofilled in the require
function.
Here is my code:
./index.js
const index = require("./src/index.js")
console.log(index) //logs the module
./src/Client.js
const index = require("./index.js") //I even tried require("../src/index.js") and the full path (D:/...)
console.log(index) //logs empty object
index.bindClient(client) //TypeError: index.bindClient is not a function
./src/index.js
module.exports = {
Send,
MessageDelete,
Command,
Client: C,
/**
*
* @param {Client} client
*/
bindClient(client) {
c = client;
return (
c.on("messageCreate", msg => {
const command = commands.find(c => msg.content.startsWith(c.name))
if (command) {
command.send ? Send(command.send, msg.channel) : null
command.delete ? MessageDelete(msg) : null
command.emit("execute", msg, command)
}
})
);
}
}
This is not my full code, I only included the important parts that relate to the error. I tried logging as much as I could and everything except for index
in ./src/Client.js
was what it was supposed to be. This error even happened when I tried to set it like module.exports.bindClient = ...
CodePudding user response:
As said by @Elitezen in their comment, the files were requiring each other. Putting the require
at the bottom and calling the function directly from it worked
require("./index.js").bindClient(client)
CodePudding user response:
You can destructure it...
const { Send, MessageDelete, Command, Client, bindClient } = require("./src/index.js")