Skip to content
Create a gist now

Instantly share code, notes, and snippets.

@gleitz /reload.js
Last active

Embed URL


Subversion checkout URL

You can clone with
Download ZIP
Reloading modules from the repl in Node.js
// Reloading modules from the repl in Node.js
// Benjamin Gleitzman (
// Inspired by Ben Barkay
// Usage: `node reload.js`
// You can load the module as usual
// var mymodule = require('./mymodule')
// And the reload it when needed
// mymodule = require.reload('./mymodule')
// I suggest using an alias in your .bashrc/.profile:
// alias node_reload='node /path/to/reload.js'
var myrepl = require("repl").start({});
* Removes a module from the cache.
myrepl.context.require.uncache = function (moduleName) {
// Run over the cache looking for the files
// loaded by the specified module name
myrepl.context.require.searchCache(moduleName, function (mod) {
delete require.cache[];
* Runs over the cache to search for all the cached files.
myrepl.context.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) {
// Call the specified callback providing the
// found module
* Load a module, clearing it from the cache if necessary.
myrepl.context.require.reload = function(moduleName) {
return myrepl.context.require(moduleName);

This code seems to not be resetting some that it should. After 'many' reloads, I get this error:

var pp = require.reload('../lib/myTestLib.js');
(node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit.
at process.EventEmitter.addListener (events.js:160:15)
at process.on.process.addListener (node.js:768:26)
at Object. (/Users/joallen/_Dev/Node.js/node_modules/log4js/lib/appenders/file.js:13:9)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at requireAppender (/Users/joallen/_Dev/Node.js/node_modules/log4js/lib/log4js.js:387:22)

I have to assume that it is something in the log4js modules that is messing things up.


What is a module "repl"?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.