var module = fileName.split('.')[0];
is the culprit.
Because of how scoping works in JavaScript, that module
variable isn't local to the for
loop but instead the whole file. Thus, when you use it at the end:
module.exports = apiRegistry;
you are setting the exports
property on your own module
variable instead of the one node expects you to use. Changing to using another variable name solves your problem:
var apiRegistry = {};
var files = fs.readdirSync('./apis');
for (var index in files) {
var fileName = files[index];
var myModule = fileName.split('.')[0];
apiRegistry[myModule] = require('../apis/' + myModule);
}
// console.log(apiRegistry) --> Prints {key: moduledef ..}
module.exports = apiRegistry;