unloading code/modules

Yes, it is possible to access the module cache directly:

var name = require.resolve('moduleName');
delete require.cache[name];

Note that if your code carries a reference to whatever was exposed by these modules you want to get rid of, it won’t be cleaned up.

(As an aside: Underneath the surface, require.resolve and require.cache are just proxies to Module._resolveFilename and Module._cache respectively, with Module being the core module loader, i.e. require('module').)

Leave a Comment