简体   繁体   English

如何从函数内修改module.exports?

[英]How to modify module.exports from within a function?

So what I mean is that I want to export a certain object within a function.所以我的意思是我想在函数中导出某个对象。

async function Set(x) {
  module.exports["x"] = x
}

This doesn't seem to work, and it becomes undefined, can you guys help?这似乎不起作用,并且变得未定义,你们能帮忙吗?

client.on('message', async message => {
    if (!message.content.startsWith(prefix) || message.author.bot) return;

    var args = message.content.split(/[ ]+/)
    const Cargs = message.content.slice(prefix.length).trim().split(/[ ]+/);
    const command = Cargs.shift().toUpperCase();

    if (client.commands.get(command)) {
        await Set(message)
        client.commands.get(command).execute()
    }
})

On the face of it, what you want to do is perfectly possible.从表面上看,您想做的事情完全有可能。

However, you need to be careful about the nature of modules and object references.但是,您需要注意模块和对象引用的性质。

For example, say we have your module file:例如,假设我们有您的模块文件:

module.js模块.js



const setFn = (x) => {
  module.exports.x = x; 
}

module.exports = {
  x: "hello", 
  setFn, 

}

And you are going to consume the export x as well as modify with the setFn function in index.js您将使用导出x以及使用 index.js 中的setFn函数进行修改

This here will not work correctly:这在这里将无法正常工作:

index.js索引.js

const {x, setFn} = require("./module"); 

console.log("Start");  //Start
console.log(x);        //hello
setFn("world");
console.log(x);        //hello - why hasn't it changed? 
console.log("end");    //end

Code Sandbox 代码沙盒

This is because you have imported a direct reference to the x variable, which has the value "hello" at the time that required it.这是因为您导入了对x变量的直接引用,该变量在需要它时的值为“hello”。

When you later mutate the module via the setFn function, you still retain that reference to the old "hello" value.当您稍后通过setFn函数setFn模块时,您仍然保留对旧“hello”值的引用。

However, if you change the code to this:但是,如果您将代码更改为:

const module = require("./module"); 

console.log("Start");  //Start
console.log(module.x);        //hello
module.setFn("world");
console.log(module.x);        //world
console.log("end");    //end

Code Sandbox代码沙盒

Then the code works.然后代码工作。

This is because instead of importing direct references to x and setFn you have imported a reference to the module itself.这是因为您没有导入对xsetFn直接引用, setFn导入了对模块本身的引用。

When you mutate the module itself, and later refer to module.x again, you can see the updated value.当您对模块本身进行变异,然后再次引用module.x时,您可以看到更新后的值。

I recommend also looking at this answer .我建议也看看这个答案 This one deals with ESM modules, but I think the lesson is the same.这个是关于 ESM 模块的,但我认为这个教训是一样的。

In terms of what you're doing though - I'm not sure how useful this is, because for this to work it really requires that the consumer of the module imports the whole module and always references the properties via module.x .就您在做什么而言 - 我不确定这有多大用处,因为要使其正常工作,它确实需要模块的使用者导入整个模块并始终通过module.x引用属性。

Also, are you sure the value you are passing into the Set function isn't undefined?另外,您确定传递给Set函数的值不是未定义的吗?

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

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