为什么不设置Nodejs模块缓存到False将Nodejs设置为重新加载模块?
我有2个文件,一个命名为一个。
var first = require("./first.js")
require.cache[first.module.id].loaded=false
require("./first.js")
console.log("First is running");
module.exports={module}
首先
First is running
。
First is running
First is running
One.js 为什么即使我将第一个模块的加载属性设置为false,它仍然没有被重新加载?
I have 2 files, one named one.js and other name first.js.
one.js
var first = require("./first.js")
require.cache[first.module.id].loaded=false
require("./first.js")
first.js
console.log("First is running");
module.exports={module}
It's output is:
First is running
and not
First is running
First is running
as I expected. Why is it that even though I set the loaded property of the first.js module to false, it is still not being reloaded?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须从
require.cache
对象中删除缓存的模块,以迫使节点再次加载模块。节点不会在require.cache
中执行该模块。从缓存中删除模块的一种方法如下:
侧面提示:您可以从模块中导出功能,然后一次又一次地调用该功能,以每次都加载模块的结果类似的结果是必须的。
You have to remove the cached module from the
require.cache
object to force node to load the module again. Node won't execute the module as long as it is in therequire.cache
.One way to delete the module from the cache is shown below:
Side Tip: You could export a function from a module and just call that function again and again to have a similar result of loading the module everytime it is required.