require-reload
require-reload facilitates hot-reloading files in node.js. Each call will re-fetch the file/module and require
it.
Example
var reload = require('require-reload')(require),
api = reload('api.js');
try {
api = reload('api.js');
} catch (e) {
console.error("Failed to reload api.js! Error: ", e);
}
Notes/Caveats
Keep in mind that the cache is shared between child modules and their parents. If you want to reload your depdencies when
you're reloaded then you must also use require-reload
. This is on purpose so things are not unintentionally reloaded.
Note: This uses internal methods to the module system without a context. These APIs may change at any time. I will keep this
maintained to support all version of Node.js >=0.6 and io.js >=1.0.4. Version management will be handled through npm.
Because of this, it is recommend you use require('require-reload')(require)
which works without any internal methods.
Advanced Usage
reload([otherContext])
If you want to run reload in the context of another module/file then pass in the require
variable into reload
to get an
instance that is bound to that context. The other module must return its require context to use this.
var otherModule = require('other-module'),
reloadInContext = require('require-reload')(otherModule.requireCtx);
emptyCache([context])
Empties the whole cache. Useful if you want to reload a file/module AND reload its dependencies. Optionally accepts a context
to clear another context's cache.