List of currently loaded node.js modules?

前端 未结 2 1599
长发绾君心
长发绾君心 2021-01-02 00:54

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

相关标签:
2条回答
  • 2021-01-02 01:08

    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.

    0 讨论(0)
  • 2021-01-02 01:23

    Check the module module source. Your answer is:

    Object.keys(require('module')._cache);
    
    0 讨论(0)
提交回复
热议问题