Skip to content

Instantly share code, notes, and snippets.

@richmarr
Created August 3, 2011 09:04
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save richmarr/1122217 to your computer and use it in GitHub Desktop.
Save richmarr/1122217 to your computer and use it in GitHub Desktop.
Export all JS modules in a directory as submodules
/*
I found it handy to reduce the amount of manual require() lines in a big
project by grouping small modules together like below.
If you put this code into "index.js" then it'll pick up any other
JS modules in that directory and expose them as sub-modules.
- Any exports in *this* module would be myPackage.exportName
- Any exports in other modules would be myPackage.moduleName.exportName
I use this for things like grouping controllers together, e.g.
controllers.whateverController.methodName
*/
var fs = require('fs');
// Read in the libs from this directory and add them as exports
// This way you can just reference
fs.readdirSync('./lib/auth').forEach(function(file){
if ( file.indexOf(".js") > -1 && file != "index.js" )
exports[ file.replace('.js','') ] = require('./'+file);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment