admin管理员组文章数量:1277281
I'm building a discord bot with node.js for my server and I have a bunch of mands for the bot. Each mand is in a different file so I have a lot of const cmd = require("../mands/cmd.js");
const kick = require("../mands/kick");
const info = require("../mands/info");
const cooldown = require("../mands/cooldown");
const help = require("../mands/help");
Is there a simpler way to do this?
I'm building a discord bot with node.js for my server and I have a bunch of mands for the bot. Each mand is in a different file so I have a lot of const cmd = require("../mands/cmd.js");
const kick = require("../mands/kick");
const info = require("../mands/info");
const cooldown = require("../mands/cooldown");
const help = require("../mands/help");
Is there a simpler way to do this?
Share Improve this question asked Apr 24, 2020 at 14:08 Forgotten-StormForgotten-Storm 3071 gold badge4 silver badges11 bronze badges3 Answers
Reset to default 8Inside folder mands
put a file called index.js
.
Each time you implement new mands in new file, require that file in index.js
and then add it to the exports of it. For example index.js would be:
const kick = require('./kick');
const info = require('./info');
module.exports = {
kick: kick,
info: info
}
And then from any folder you can require multiple mands in one line like this:
const { kick, info } = require('../mands');
Export an object from one file instead?
const kick = require("../mands/kick");
const info = require("../mands/info");
const cooldown = require("../mands/cooldown");
const help = require("../mands/help");
const mands = {
kick,
info,
...
}
module.exports = mands;
And then:
const mands = require('mymands')
mands.kick()
Create index.js
file inside the mand
folder and then you can export an object like this.
const kick = require("../mands/kick");
const info = require("../mands/info");
const cooldown = require("../mands/cooldown");
const help = require("../mands/help");
const mand = {
kick,
info,
cooldown,
help
};
module.exports = mand;
You can import and use it like this:
const {kick, info} = require('./mands');
本文标签: javascriptNodejs require multiple files in the same folderStack Overflow
版权声明:本文标题:javascript - Node.js require multiple files in the same folder - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741282346a2370079.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论