UNPKG

1.96 kBMarkdownView Raw
1# require-reload #
2
3require-reload facilitates hot-reloading files in node.js. Each call will re-fetch the file/module and `require` it.
4
5## Example ##
6```JS
7//things will work just the same with require('require-reload') but see note after this example
8var reload = require('require-reload')(require),
9 api = reload('api.js');
10//sometime later if you make changes to api.js, you can hot-reload it by calling
11//this could also just be in a setInterval
12try {
13 api = reload('api.js');
14} catch (e) {
15 //if this threw an error, the api variable is still set to the old, working version
16 console.error("Failed to reload api.js! Error: ", e);
17}
18```
19
20## Notes/Caveats ##
21Keep in mind that the cache is shared between child modules and their parents. If you want to reload your depdencies when
22you're reloaded then you must also use `require-reload`. This is on purpose so things are not unintentionally reloaded.
23
24Note: This uses internal methods to the module system without a context. These APIs may change at any time. I will keep this
25maintained to support all version of Node.js >=0.6 and io.js >=1.0.4. Version management will be handled through npm.
26
27**Because of this, it is recommend you use `require('require-reload')(require)` which works without any internal methods.**
28
29## Advanced Usage ##
30
31### reload([otherContext]) ###
32If you want to run reload in the context of another module/file then pass in the `require` variable into `reload` to get an
33instance that is bound to that context. The other module must return its require context to use this.
34```JS
35var otherModule = require('other-module'),
36 reloadInContext = require('require-reload')(otherModule.requireCtx);
37/*
38 * other-module would need to do:
39 * exports.requireCtx = require;
40 */
41```
42
43### emptyCache([context]) ###
44Empties the whole cache. Useful if you want to reload a file/module AND reload its dependencies. Optionally accepts a context
45to clear another context's cache.