1

Is it possible to unload modules in nodejs?
In other words: clear event listeners, timeouts, and intervals.

These modules are "sub-files" of my project, and i could overwrite the .on(), and .once(), but what about timeouts and intervals?

MiniGod
  • 3,683
  • 1
  • 26
  • 27
  • I'm pretty sure that it is possible to implement something that does this. This is related btw: http://stackoverflow.com/questions/6676612/unloading-code-modules – Timo Huovinen Oct 05 '13 at 06:33

1 Answers1

0

No, not that I'm aware of.

You can remove all event listeners on an emitter by calling myEmitter.removeAllListeners(). As for clearing the timeouts and intervals, call clearTimeout(timeoutName) and clearInterval(intervalName) respectively.

Example:

var x = 0;
var myInterval = setInterval(function(){
  console.log('hello');
  if (x > 5) clearInterval(myInterval);
  x += 1;
},1000);

Hopefully this helps.

JP Richardson
  • 38,609
  • 36
  • 119
  • 151
  • Wouldn't really work. The `modules` I'm talking about are kinda like `plugins`, and i want to be able to load (require), and unload the plugins without restarting the system. I don't have a list of all the timeouts and intervals, and all the plugins listen to the same emitter. `removeAllListener` would remove all listeners for the other plugins too... Nice try though, ty. – MiniGod Oct 09 '12 at 02:11
  • 1
    You're going to have to show some code or be more specific. Your description of the problem isn't entirely clear. Each module and plugin should be self contained.... so why can't you implement like a `stop` method on them that clears events, intervals, timeouts, etc? – JP Richardson Oct 09 '12 at 02:13