本文翻译自:node.js require() cache - possible to invalidate?

From the node.js documentation: 从node.js文档中:

Modules are cached after the first time they are loaded. 第一次加载模块后将对其进行缓存。 This means (among other things) that every call to require('foo') will get exactly the same object returned, if it would resolve to the same file. 这意味着(除其他事项外)每次对require('foo')的调用都将返回完全相同的对象,如果它可以解析为相同的文件。

Is there a way to invalidate this cache? 有没有办法使此缓存无效? ie for unit testing, I'd like each test to be working on a fresh object. 即对于单元测试,我希望每个测试都可以在一个新对象上进行。


#1楼

参考:https://stackoom.com/question/ce58/node-js-require-缓存-可能无效


#2楼

You can always safely delete an entry in require.cache without a problem, even when there are circular dependencies. 即使存在循环依赖关系,也始终可以安全地删除require.cache中的条目,而不会出现问题。 Because when you delete, you just delete a reference to the cached module object, not the module object itself, the module object will not be GCed because in case of circular dependencies, there is still a object referencing this module object. 因为在删除时,您仅删除对缓存的模块对象的引用,而不是对模块对象本身的引用,所以不会对GC对象进行GC,因为在循环依赖的情况下,仍然有一个对象引用此模块对象。

Suppose you have: 假设您有:

script a.js: 脚本a.js:

var b=require('./b.js').b;
exports.a='a from a.js';
exports.b=b;

and script b.js: 脚本b.js:

var a=require('./a.js').a;
exports.b='b from b.js';
exports.a=a;

when you do: 当您这样做时:

var a=require('./a.js')
var b=require('./b.js')

you will get: 你会得到:

> a
{ a: 'a from a.js', b: 'b from b.js' }
> b
{ b: 'b from b.js', a: undefined }

now if you edit your b.js: 现在,如果您编辑b.js:

var a=require('./a.js').a;
exports.b='b from b.js. changed value';
exports.a=a;

and do: 并做:

delete require.cache[require.resolve('./b.js')]
b=require('./b.js')

you will get: 你会得到:

> a
{ a: 'a from a.js', b: 'b from b.js' }
> b
{ b: 'b from b.js. changed value',a: 'a from a.js' }

#3楼

Yes, you can access the cache via require.cache[moduleName] where moduleName is the name of the module you wish to access. 是的,您可以通过require.cache[moduleName]访问缓存,其中moduleName是您要访问的模块的名称。 Deleting an entry by calling delete require.cache[moduleName] will cause require to load the actual file. 通过调用delete require.cache[moduleName]删除条目将导致require加载实际文件。

This is how you would remove all cached files associated with the module: 这是删除与该模块关联的所有缓存文件的方式:

/*** Removes a module from the cache*/
function purgeCache(moduleName) {// Traverse the cache looking for the files// loaded by the specified module namesearchCache(moduleName, function (mod) {delete require.cache[mod.id];});// Remove cached paths to the module.// Thanks to @bentael for pointing this out.Object.keys(module.constructor._pathCache).forEach(function(cacheKey) {if (cacheKey.indexOf(moduleName)>0) {delete module.constructor._pathCache[cacheKey];}});
};/*** Traverses the cache to search for all the cached* files of the specified module name*/
function searchCache(moduleName, callback) {// Resolve the module identified by the specified namevar mod = require.resolve(moduleName);// Check if the module has been resolved and found within// the cacheif (mod && ((mod = require.cache[mod]) !== undefined)) {// Recursively go over the results(function traverse(mod) {// Go over each of the module's children and// traverse themmod.children.forEach(function (child) {traverse(child);});// Call the specified callback providing the// found cached modulecallback(mod);}(mod));}
};

Usage would be: 用法是:

// Load the package
var mypackage = require('./mypackage');// Purge the package from cache
purgeCache('./mypackage');

Since this code uses the same resolver require does, just specify whatever you would for require. 由于该代码使用同一解析器require做,只需指定任何你想做的需要。


"Unix was not designed to stop its users from doing stupid things, as that would also stop them from doing clever things." “ Unix并非旨在阻止其用户执行愚蠢的事情,因为这也将阻止他们执行聪明的事情。” – Doug Gwyn –道格·格温(Doug Gwyn)

I think that there should have been a way for performing an explicit uncached module loading. 我认为应该有一种方法可以执行显式的未缓存模块加载。


#4楼

If you always want to reload your module, you could add this function: 如果您始终想重新加载模块,则可以添加以下功能:

function requireUncached(module){delete require.cache[require.resolve(module)]return require(module)
}

and then use requireUncached('./myModule') instead of require. 然后使用requireUncached('./myModule')代替require。


#5楼

I'd add to luff's answer one more line and change the parameter name: 我会再向luff的答案添加一行,并更改参数名称:

function requireCached(_module){var l = module.children.length;for (var i = 0; i < l; i++){if (module.children[i].id === require.resolve(_module)){module.children.splice(i, 1);break;}}delete require.cache[require.resolve(_module)];return require(_module)
}

#6楼

rewire is great for this use case, you get a new instance with each call. rewire非常适合此用例,每次调用都会获得一个新实例。 Easy dependency injection for node.js unit testing. 轻松的依赖注入,用于node.js单元测试。

rewire adds a special setter and getter to modules so you can modify their behaviour for better unit testing. rewire在模块中添加了一个特殊的setter和getter,因此您可以修改其行为以进行更好的单元测试。 You may 你可以

inject mocks for other modules or globals like process leak private variables override variables within the module. 为其他模块或全局变量(例如进程泄漏专用变量)注入模拟,将覆盖模块内的变量。 rewire does not load the file and eval the contents to emulate node's require mechanism. rewire不会加载文件并评估内容以模拟节点的require机制。 In fact it uses node's own require to load the module. 实际上,它使用节点自身的要求来加载模块。 Thus your module behaves exactly the same in your test environment as under regular circumstances (except your modifications). 因此,您的模块在测试环境中的行为与正常情况下完全一样(修改除外)。

Good news to all caffeine-addicts: rewire works also with Coffee-Script. 对所有咖啡因上瘾者来说是个好消息:rewire也可以在Coffee-Script中使用。 Note that in this case CoffeeScript needs to be listed in your devDependencies. 请注意,在这种情况下,需要在devDependencies中列出CoffeeScript。

node.js require()缓存-可能无效?相关推荐

  1. Node.js Buffers缓存对象

    Node.js Buffers缓存对象 纯粹的 Javascript 对 Unicode 很友好,但是操作二进制数据就不怎么在行了.处理 TCP 数据流或者文件时,必须要操作二进制数据流. node ...

  2. node.js清除缓存命令

    转载自:http://blog.csdn.net/DayDayUpTianTian/article/details/77814817 安装node.js的web3库的时候报错,记录下解决方法,先清除下 ...

  3. node.js require 自动执行脚本 并生成html,利用node.js实现自动生成前端项目组件的方法详解...

    本文主要给大家介绍了关于利用node.js实现自动生成前端项目组件的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍: 脚本编写背景 写这个小脚本的初衷是,项目本身添加一个组件太 ...

  4. 用Node.js申请缓存buffer报DEP0005错误的解决方法

    现象 用node.js写的程序中,有如下语句: var buf = new Buffer(myBase64Str, 'base64'); 其中,myBase64Str 是已有的一个base64字符串. ...

  5. node.js require 自动执行脚本 并生成html,nodejs 执行脚本并实时输出

    接到需求 需要一个服务来执行shell脚本,要求可以实时打印shell脚本执行的过程,并看到脚本执行的结果. 明确任务目标: 这是一个web服务,需要执行shell脚本 当一个脚本执行的时候,再次发送 ...

  6. node.js require 自动执行脚本 并生成html,从HTML页面执行Nodejs脚本?

    小编典典 我使用普通的JS而非咖啡脚本,因此这是每个Fosco注释(称为server.js)的示例: var express = require('express'), list = require( ...

  7. NGINX配置基于Node.js服务的负载均衡服务器

    NGINX配置基于Node.js服务的负载均衡服务器 本部署指南说明了如何使用NGINX开源和NGINX Plus在Node.js应用程序服务器池之间平衡HTTP和HTTPS通信.本指南中的详细说明适 ...

  8. Node.js毕业设计——基于Node.js+JavaScript+MongoDB的供求信息网站设计与实现(毕业论文+程序源码)——供求信息网站

    基于Node.js+JavaScript+MongoDB的供求信息网站设计与实现(毕业论文+程序源码) 大家好,今天给大家介绍基于Node.js+JavaScript+MongoDB的供求信息网站设计 ...

  9. 编写原生的Node.js模块

    通常,我们开发原生Node.js模块包括但不仅限于以下原因: 对性能有比较苛刻要求的应用.尽管Node.js得益于libuv,在异步I/O操作很有优势,但遇到数字计算时并不是一个很好的选择. 使用更加 ...

最新文章

  1. KNN(k-NearestNeighbor)
  2. 怎么用feign远程调用别人的接口_spring cloud-openFeign声明式远程调用
  3. centos7配网卡_centos7配置网卡
  4. no ip domain-lookup 什么意思
  5. 使用SWAGGER和ASP.NET CORE设置可选路由参数
  6. 推荐一个值得加入C++开发者俱乐部
  7. Android一个漂亮的日历组件源码
  8. 70. Climbing Stairs【leetcode】递归,动态规划,java,算法
  9. 使用pynput模块监听用户键盘输入,保存至txt
  10. 颜值与特色并存!各大高校“中秋限定款”月饼刷屏,你酸了吗?
  11. easytrader 量化交易平台连接同花顺软件使用平安证券
  12. Python数据分析-房价的影响因素图解
  13. php 写入exif,用PHP将EXIF写入JPG
  14. 看板(Kanban)与Scrum区别
  15. Python3运行web.py测试,出现RuntimeError: generator raised StopIteration异常
  16. 【Unity】用Lerp()实现类杀戮尖塔手牌变化
  17. python数据统计分析
  18. 【附源码】计算机毕业设计java在线学习系统设计与实现
  19. 《Java 8实战》
  20. 项目_MySQL比较字符大小的小坑

热门文章

  1. 打印一个字符串的所有排列。
  2. Java Thead.interrupt 方法没有使线程停止工作
  3. 算法------买卖股票的最佳时机
  4. Windows usb设备正在使用中
  5. Android10.0 Binder通信原理(一)Binder、HwBinder、VndBinder概要
  6. Android10.0系统启动之Launcher(桌面)启动流程-[Android取经之路]
  7. cordova入门——cordova环境配置
  8. opencv补全边缘_为什么OpenCV中绘制的轮廓不能填充图像边缘的轮廓?
  9. NSPredicate的使用
  10. Flutter开发之《头条 Flutter iOS 混合工程实践》笔记(54)