I find that it works great the way you describe.
You can do a little work by adding the index.js file to the directory and exporting other files from the same directory. Then require directory to get them all.
// add.js module.exports = function (num1, num2) { return num1 + num2; }; // subtract.js module.exports = function (num1, num2) { return num1 - num2; }; // multiply.js module.exports = function (num1, num2) { return num1 * num2; }; // index.js var Calc = {}; require('fs').readdirSync(__dirname).forEach(function (file) { if (file !== 'index.js') { var fileName = file.replace('.js', ''); Calc[fileName] = require('./' + fileName); } }); module.exports = Calc; // main.js var Calc = require('./calc'); var zero = Calc.subtract(1, 1); var one = Calc.multiply(1, 1); var two = Calc.add(1, 1);
With the following file structure:
├── calc │ ├── add.js │ ├── index.js │ ├── multiply.js │ └── subtract.js └── main.js
source share