Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of currently loaded node.js modules?

Tags:

node.js

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...

like image 888
Udo G Avatar asked Mar 20 '12 17:03

Udo G


2 Answers

Check the module module source. Your answer is:

Object.keys(require('module')._cache);
like image 134
Anatoliy Avatar answered Nov 10 '22 10:11

Anatoliy


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.

    • To test if a module is a core module, use 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.

like image 39
mklement0 Avatar answered Nov 10 '22 10:11

mklement0