0

Help me please.

I want require all files from directory ? How best to do it?

I read about require_paths but it does not perform those functions.

Michael Phelps
  • 3,451
  • 7
  • 36
  • 64

1 Answers1

0

You can walk through a directory and require each one:

var mods = {};
var files = fs.readdirSync('your_directory').filter(function(x) { return x.substr(-3) == ".js"; });
for(var i = 0; i != files.length;++i) {
    mods[files[i]] = require(path.join('your_directory',files[i]));
}
SheetJS
  • 22,470
  • 12
  • 65
  • 75