Haven't you ever always needed to know what files are being required by your app? Okay, maybe not always, but once? No? Just me? Fine, move along then. But in case you need it, here's how it all works:
It's in npm, of course:
npm install runtime-required
require('runtime-required')
→ EventEmitter
Yup, you just include it in your project and it exposes an event emitter. There is only a single file
event, and it has the following properties:
type
{string}: one ofbuiltin
(node's default modules)module
(ones that appear inside thenode_modules
directory)file
(from your own project... or I guess elsewhere on the filesystem)
id
{string}: the identifier for this module. Formodule
andfile
types, it will be a fully-resolved file path. Forbuiltin
types, it will be the name of the module.
const required = require('runtime-required');
required.on('file', data => {
console.log(`a ${data.type} module was required at "${data.id}"`);
});