1

I'm really a big fan of Swagger's node module, but one thing is driving me crazy:

The sample app contains the following line (api/controller/hello_world.js):

var util = require('util');

But I just can't find this module. I tried to

  • list it with npm list but nothing
  • search for it with Spotlight (util.js)

My question is: How can I list the actually loaded modules in nodejs?

mklement0
  • 382,024
  • 64
  • 607
  • 775
boj
  • 10,935
  • 5
  • 38
  • 57

2 Answers2

2

util is part of the node.js distribution: https://nodejs.org/api/util.html

TimWolla
  • 31,849
  • 8
  • 63
  • 96
2

TimWolla's answer solves your particular problem with respect to the util module.

As for the more generic question:

How can I list the actually loaded modules in nodejs?

  • This answer tells you how to list the full filenames of currently loaded non-core modules (spoiler alert: Object.keys(require.cache).

  • For core modules - modules that come with Node.js itself, such as util - there is no way that I know of that tells you which ones are loaded, but given that they're core, they may all implicitly be loaded, for all we know (and should care).

  • That said, to test if a given module is a core module, you can use require.resolve(), which returns the input module name as-is (rather than a full filename) in the case of core modules; e.g.:

    require.resolve('util') // -> 'util', i.e. NOT A PATH -> core module
    

Note: When require.resolve() returns a filesystem path, the implication is merely that the module is non-core and can be located; it does not tell you whether or not that module is currently loaded (use require.cache for that).

Community
  • 1
  • 1
mklement0
  • 382,024
  • 64
  • 607
  • 775