Skip to main content
Improved the code and grammar
Source Link
Ben Barkay
  • 5.7k
  • 2
  • 23
  • 30
/** * Removes a module from the cache */ require.uncache = function purgeCache(moduleName) { // Run overTraverse the cache looking for the files // loaded by the specified module name require.searchCache(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]; } }); }; /** * Runs overTraverses the cache to search for all the cached * files of the specified module name */ require.searchCache = function searchCache(moduleName, callback) { // Resolve the module identified by the specified name var mod = require.resolve(moduleName); // Check if the module has been resolved and found within // the cache if (mod && ((mod = require.cache[mod]) !== undefined)) { // Recursively go over the results (function runtraverse(mod) { // Go over each of the module's children and // run overtraverse itthem mod.children.forEach(function (child) { runtraverse(child); }); // Call the specified callback providing the // found cached module callback(mod); })(mod)); } }; 

For instance, usageUsage would be:

// Load the package var mypackage = require('./mypackage'); // UncachePurge the package from cache require.uncachepurgeCache('./mypackage'); 

I think that there should behave been a way for performing an implied non-cachedexplicit uncached module loading.

/** * Removes a module from the cache */ require.uncache = function (moduleName) { // Run over the cache looking for the files // loaded by the specified module name require.searchCache(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]; } }); }; /** * Runs over the cache to search for all the cached * files */ require.searchCache = function (moduleName, callback) { // Resolve the module identified by the specified name var mod = require.resolve(moduleName); // Check if the module has been resolved and found within // the cache if (mod && ((mod = require.cache[mod]) !== undefined)) { // Recursively go over the results (function run(mod) { // Go over each of the module's children and // run over it mod.children.forEach(function (child) { run(child); }); // Call the specified callback providing the // found module callback(mod); })(mod); } }; 

For instance, usage would be:

// Load the package var mypackage = require('./mypackage'); // Uncache the package require.uncache('./mypackage'); 

I think there should be a way for performing an implied non-cached module loading.

/** * Removes a module from the cache */ function purgeCache(moduleName) { // Traverse the cache looking for the files // loaded by the specified module name searchCache(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 name var mod = require.resolve(moduleName); // Check if the module has been resolved and found within // the cache if (mod && ((mod = require.cache[mod]) !== undefined)) { // Recursively go over the results (function traverse(mod) { // Go over each of the module's children and // traverse them mod.children.forEach(function (child) { traverse(child); }); // Call the specified callback providing the // found cached module callback(mod); }(mod)); } }; 

Usage would be:

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

I think that there should have been a way for performing an explicit uncached module loading.

added 328 characters in body
Source Link
Ben Barkay
  • 5.7k
  • 2
  • 23
  • 30
/** * Removes a module from the cache */ require.uncache = function (moduleName) { // Run over the cache looking for the files // loaded by the specified module name require.searchCache(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]; } }); }; /** * Runs over the cache to search for all the cached * files */ require.searchCache = function (moduleName, callback) { // Resolve the module identified by the specified name var mod = require.resolve(moduleName); // Check if the module has been resolved and found within // the cache if (mod && ((mod = require.cache[mod]) !== undefined)) { // Recursively go over the results (function run(mod) { // Go over each of the module's children and // run over it mod.children.forEach(function (child) { run(child); }); // Call the specified callback providing the // found module callback(mod); })(mod); } }; 
/** * Removes a module from the cache */ require.uncache = function (moduleName) { // Run over the cache looking for the files // loaded by the specified module name require.searchCache(moduleName, function (mod) { delete require.cache[mod.id]; }); }; /** * Runs over the cache to search for all the cached * files */ require.searchCache = function (moduleName, callback) { // Resolve the module identified by the specified name var mod = require.resolve(moduleName); // Check if the module has been resolved and found within // the cache if (mod && ((mod = require.cache[mod]) !== undefined)) { // Recursively go over the results (function run(mod) { // Go over each of the module's children and // run over it mod.children.forEach(function (child) { run(child); }); // Call the specified callback providing the // found module callback(mod); })(mod); } }; 
/** * Removes a module from the cache */ require.uncache = function (moduleName) { // Run over the cache looking for the files // loaded by the specified module name require.searchCache(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]; } }); }; /** * Runs over the cache to search for all the cached * files */ require.searchCache = function (moduleName, callback) { // Resolve the module identified by the specified name var mod = require.resolve(moduleName); // Check if the module has been resolved and found within // the cache if (mod && ((mod = require.cache[mod]) !== undefined)) { // Recursively go over the results (function run(mod) { // Go over each of the module's children and // run over it mod.children.forEach(function (child) { run(child); }); // Call the specified callback providing the // found module callback(mod); })(mod); } }; 
Rollback to Revision 4
Source Link
Ben Barkay
  • 5.7k
  • 2
  • 23
  • 30

Yes, you can access the cache via require.cache[moduleName] where moduleName is the name of the module you wish to access. Deleting an entry by calling delete require.cache[moduleName] will invalidate the cache and cause require to load it againthe actual file.

HereThis is a snippet I wrote for my own use, it will uncachehow you would remove all cached files associated with the module and all of its dependencies:

/** * Removes a module from the cache */ require.uncache = function uncacheModule(moduleName) { // Run over the cache looking for the files // loaded by the specified module name require.searchCache(moduleName, function (mod) { delete require.cache[mod.id]; }); }; /** * Runs over the cache to search for all the cached * files */ require.searchCache = function (moduleName, callback) { // Resolve the module identified by the specified name var mod = require.resolve(moduleName); // Check if the module has been resolved and found within // the cache if (mod && ((mod = require.cache[mod]) !== undefined)) { // Recursively go over the results (function run(mod) { // Go over each of the module's children and // run over it mod.children.forEach(function (child) { run(child); }); // Call the specified callback providing the // found module callback(mod); })(mod); } }; 

For instance, usage would be:

// Load the package var mypackage = require('./mypackage'); // Uncache the package require.uncache('./mypackage'); 

Since this code uses the same resolver require does, just specify whatever you would for require.


"Unix was not designed to stop its users from doing stupid things, as that would also stop them from doing clever things." – Doug Gwyn

In my opinion,I think there should be a way for performing an implied non-cached module loading should have been made available by Node.js

Yes, you can access the cache via require.cache[moduleName] where moduleName is the name of the module you wish to access. Deleting an entry by calling delete require.cache[moduleName] will invalidate the cache and cause require to load it again.

Here is a snippet I wrote for my own use, it will uncache the module and all of its dependencies:

/** * Removes a module from the cache */ function uncacheModule(moduleName) { // Run over the cache looking for the files // loaded by the specified module name require.searchCache(moduleName, function (mod) { delete require.cache[mod.id]; }); }; /** * Runs over the cache to search for all the cached * files */ require.searchCache = function (moduleName, callback) { // Resolve the module identified by the specified name var mod = require.resolve(moduleName); // Check if the module has been resolved and found within // the cache if (mod && ((mod = require.cache[mod]) !== undefined)) { // Recursively go over the results (function run(mod) { // Go over each of the module's children and // run over it mod.children.forEach(function (child) { run(child); }); // Call the specified callback providing the // found module callback(mod); })(mod); } }; 

For instance, usage would be:

// Load the package var mypackage = require('./mypackage'); // Uncache the package require.uncache('./mypackage'); 

Since this code uses the same resolver require does, just specify whatever you would for require.


"Unix was not designed to stop its users from doing stupid things, as that would also stop them from doing clever things." – Doug Gwyn

In my opinion, a way for performing an implied non-cached module loading should have been made available by Node.js

Yes, you can access the cache via require.cache[moduleName] where moduleName is the name of the module you wish to access. Deleting an entry by calling delete require.cache[moduleName] will cause require to load the actual file.

This is how you would remove all cached files associated with the module:

/** * Removes a module from the cache */ require.uncache = function (moduleName) { // Run over the cache looking for the files // loaded by the specified module name require.searchCache(moduleName, function (mod) { delete require.cache[mod.id]; }); }; /** * Runs over the cache to search for all the cached * files */ require.searchCache = function (moduleName, callback) { // Resolve the module identified by the specified name var mod = require.resolve(moduleName); // Check if the module has been resolved and found within // the cache if (mod && ((mod = require.cache[mod]) !== undefined)) { // Recursively go over the results (function run(mod) { // Go over each of the module's children and // run over it mod.children.forEach(function (child) { run(child); }); // Call the specified callback providing the // found module callback(mod); })(mod); } }; 

For instance, usage would be:

// Load the package var mypackage = require('./mypackage'); // Uncache the package require.uncache('./mypackage'); 

Since this code uses the same resolver require does, just specify whatever you would for require.


"Unix was not designed to stop its users from doing stupid things, as that would also stop them from doing clever things." – Doug Gwyn

I think there should be a way for performing an implied non-cached module loading.

added 68 characters in body
Source Link
Ben Barkay
  • 5.7k
  • 2
  • 23
  • 30
Loading
added 240 characters in body
Source Link
Ben Barkay
  • 5.7k
  • 2
  • 23
  • 30
Loading
added 2 characters in body
Source Link
Ben Barkay
  • 5.7k
  • 2
  • 23
  • 30
Loading
added 326 characters in body
Source Link
Ben Barkay
  • 5.7k
  • 2
  • 23
  • 30
Loading
Source Link
Ben Barkay
  • 5.7k
  • 2
  • 23
  • 30
Loading