2

Any thoughts on how I could get an array of file paths required (recursively) by a Node script? For example, given:

a.js

module.exports = {};

b.js

module.exports = {
   a : require('./a.js');
};

c.js

const b = require('./b.js');

//etc. etc.

I'd like to run something like:

someAwesomeFunctionOrLibrary(require('./c.js'));  // ["./b.js","./a.js"]
Dan
  • 6,022
  • 3
  • 20
  • 28
  • 1
    Related: [How can I see the full nodejs “require()” tree starting at a given file?](https://stackoverflow.com/questions/30548132/how-can-i-see-the-full-nodejs-require-tree-starting-at-a-given-file) – [dependency-tree](https://www.npmjs.com/package/dependency-tree) seems to be another option. – Jonathan Lonowski Feb 02 '19 at 22:38

1 Answers1

0

A simple solution would be to traverse module.children recursively.

var listModulePaths = (mod) => {
  for (var child of mod.children) {
    console.log(child.id)
    listModulePaths(child)
  }
}

listModulePaths(module)
fzzle
  • 1,466
  • 5
  • 23
  • 28