【发布时间】:2020-07-15 13:05:00
【问题描述】:
我有一个我想要导出 的 node.js 模块,其中包含多个功能。其中许多功能需要一个通用模块,如下面的代码所示:
module.exports = {
a: function () {
const util = require("commonModule");
// Do things
},
b: function () {
const util = require("commonModule");
// Do other things
},
c: function () {
const util = require("commonModule");
// Do more other things
}
}
如果我在 module.exports 格式中没有这个,我可以简单地执行以下操作并导入该模块一次,它将可用于所有功能:
const util = require("commonModule");
function a(){
// Do things using commonModule
}
function b(){
// Do other things using commonModule
}
有没有办法修改 module.exports 版本,这样当用户导入我的模块时,它会自动导入 commonModule 并为所有函数提供它,而不是让每个函数调用导入一个新的 commonModule 实例?
【问题讨论】:
标签: javascript node.js module node-modules