Is there a function in node.js that gives me all currently loaded modules - or even better, their filenames?
I want to restart my process as soon as a loaded module gets updated and so need to know which files need to be monitored for changes...
Check the module
module source. Your answer is:
Object.keys(require('module')._cache);
To complement Anatoliy's helpful answer:
Since Node.js v0.3.0, require.cache
contains the loaded non-core modules:
Object.keys(require.cache); # lists full filenames of loaded non-core modules
The advantage of require.cache
is that it is documented, unlike module._cache
.
Note:
Core modules such as util
do NOT show up in require.cache
.
require.resolve(<name>)
: if it returns <name>
, i.e., merely the module name itself, the module is core; non-core modules will return their full filename.require.cache
contains a hash of all currently loaded non-core modules as module
instances, keyed by their full filenames.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With