繁体   English   中英

如何从另一个文件 javascript 导入 Module.exports

[英]How to import Module.exports from another file javascript

我正在尝试从其模块导入文件。 导出以便我可以从中获取功能等。 我现在从我的 bot.js 文件中有这个(这只是导出和文件的构造函数部分。完整文件在这里: https ://pastebin.com/kA39fsPV)

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

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


这是 index.js。 我正在尝试从经济客户端内部导入功能。

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

我认为我导入的方式不正确,但我不确定如何正确导入功能和其他部分。 我将如何正确地做到这一点?

因为您直接将类分配给module.exports中的bot.jsbot.js是一个类,而不是在 require 语句中返回的对象。

这意味着您不需要使用EconomyClient.EconomyClient

你的index.js文件应该是这样的

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

您导入的方式没问题,但我认为这是由于您在 index.js 文件中启动客户端实例的方式。 应该是这样的——

const client = new EconomyClient()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM