Bob joe12
Bob joe12

Reputation: 125

How to import Module.exports from another file javascript

I am trying to import a file from its module. exports so I can get functions etc from it. I have this right now from my bot.js file (This is just exported and Constructor part of the file. Full file here: https://pastebin.com/kA39fsPV)

const { Client, Collection } = require("discord.js");

class EconomyClient extends Client {
    constructor() {
        super();
}
}
module.exports = EconomyClient;


Here is index.js. I am trying to import functions from inside Economy Client.

const EconomyClient = require(`./bot.js`);
const client = EconomyClient.EconomyClient

I think the way I am importing this is incorrect, but I am not sure how to import the functions and other parts properly. How would I do that properly?

Upvotes: 1

Views: 3188

Answers (2)

Agiel
Agiel

Reputation: 46

note that when you declare class, if you call the class without "new" its means you cannot call the constructor without new Class() Because class is not a variable , the javascript think that is a variable or function name you just export. const client = new myclass(param)

module.exports = client

Or Just wrap like this on your project

module.exports = class EconomyClient extends Client {
    constructor() {
        super();
    }
}

Upvotes: 1

Md. Shahla Zulkarnine
Md. Shahla Zulkarnine

Reputation: 56

The way you are importing is okay but I think it's due to the way you are initiating the client instance in the index.js file. It should be like this -

const client = new EconomyClient()

Upvotes: 1

Related Questions