在Nodejs中删除caching意味着什么?

请在下面findnodejs中的示例代码:

var hello_file = require.resolve('hello') var hello = require('hello') console.log(m.hello()); // there is a method hello in module hello.js delete require.cache[hello_file] console.log(m.hello()); // it still works 

我以为删除将删除对模块的引用,因此最后一行应该会引发错误。 但事实并非如此。 可能是什么原因,删除caching究竟意味着什么?

caching不知道它了,但您的var hello仍然有一个以前加载的引用。

当你下次调用require('hello')它会从文件中加载模块。 但是,直到你更新了var hello持有的引用,它将继续指向最初加载的模块。

如你所知,即使你需要很多次,节点也会加载一个模块,模块会在第一次被加载后被caching。 如果从caching中删除它,则会在下次需要时将该模块从文件系统重新加载到caching中。