为什么不设置Nodejs模块缓存到False将Nodejs设置为重新加载模块?

发布于 2025-01-28 16:36:27 字数 448 浏览 1 评论 0原文

我有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦断已成空 2025-02-04 16:36:27

您必须从require.cache对象中删除缓存的模块,以迫使节点再次加载模块。节点不会在require.cache中执行该模块。

从缓存中删除模块的一种方法如下:

delete require.cache[require.resolve("./first.js")];

侧面提示:您可以从模块中导出功能,然后一次又一次地调用该功能,以每次都加载模块的结果类似的结果是必须的。

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 the require.cache.

One way to delete the module from the cache is shown below:

delete require.cache[require.resolve("./first.js")];

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文