title: How to remove module after "require" in node.js?
body: <p>Let say, after I require a module and do something as below:</p>
  
  <pre><code>var b = require('./b.js');
  --- do something with b ---
  </code></pre>
  
  <p>Then I want to take away module b (i.e. clean up the cache).  how I can do it?</p>
  
  <p>The reason is that I want to dynamically load/ remove or update the module without restarting node server.  any idea?</p>
  
  <p>------- more --------
  based on the suggestion to delete require.cache, it still doesn't work...</p>
  
  <pre><code>what I did are few things:
  1) delete require.cache[require.resolve('./b.js')];
  2) loop for every require.cache's children and remove any child who is b.js
  3) delete b
  </code></pre>
  
  <p>However, when i call b, it is still there!   it is still accessible. unless I do that:</p>
  
  <pre><code>b = {};
  </code></pre>
  
  <p>not sure if it is a good way to handle that. 
  because if later, I require ('./b.js') again while b.js has been modified.  Will it require the old cached b.js (which I tried to delete), or the new one?</p>
  
  <p>----------- More finding --------------</p>
  
  <p>ok. i do more testing and playing around with the code.. here is what I found:</p>
  
  <pre><code>1) delete require.cache[]  is essential.  Only if it is deleted, 
   then the next time I load a new b.js will take effect.
  2) looping through require.cache[] and delete any entry in the 
   children with the full filename of b.js doesn't take any effect.  i.e.
  u can delete or leave it.  However, I'm unsure if there is any side
  effect.  I think it is a good idea to keep it clean and delete it if
  there is no performance impact.
  3) of course, assign b={} doesn't really necessary, but i think it is 
   useful to also keep it clean.
  </code></pre>