/** * 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.