3 Recursively iterates over specified directory, `require()`'ing each file, and returning a nested hash structure containing those modules.
5 **[Follow me (@troygoode) on Twitter!](https://twitter.com/intent/user?screen_name=troygoode)**
7 [![NPM](https://nodei.co/npm/require-directory.png?downloads=true&stars=true)](https://nodei.co/npm/require-directory/)
9 [![build status](https://secure.travis-ci.org/troygoode/node-require-directory.png)](http://travis-ci.org/troygoode/node-require-directory)
13 ### Installation (via [npm](https://npmjs.org/package/require-directory))
16 $ npm install require-directory
21 A common pattern in node.js is to include an index file which creates a hash of the files in its current directory. Given a directory structure like so:
32 `routes/index.js` uses `require-directory` to build the hash (rather than doing so manually) like so:
35 var requireDirectory = require('require-directory');
36 module.exports = requireDirectory(module);
39 `app.js` references `routes/index.js` like any other module, but it now has a hash/tree of the exports from the `./routes/` directory:
42 var routes = require('./routes');
46 app.get('/', routes.home);
47 app.get('/register', routes.auth.register);
48 app.get('/login', routes.auth.login);
49 app.get('/logout', routes.auth.logout);
52 The `routes` variable above is the equivalent of this:
56 home: require('routes/home.js'),
58 login: require('routes/auth/login.js'),
59 logout: require('routes/auth/logout.js'),
60 register: require('routes/auth/register.js')
65 *Note that `routes.index` will be `undefined` as you would hope.*
67 ### Specifying Another Directory
69 You can specify which directory you want to build a tree of (if it isn't the current directory for whatever reason) by passing it as the second parameter. Not specifying the path (`requireDirectory(module)`) is the equivelant of `requireDirectory(module, __dirname)`:
72 var requireDirectory = require('require-directory');
73 module.exports = requireDirectory(module, './some/subdirectory');
76 For example, in the [example in the Usage section](#usage) we could have avoided creating `routes/index.js` and instead changed the first lines of `app.js` to:
79 var requireDirectory = require('require-directory');
80 var routes = requireDirectory(module, './routes');
85 You can pass an options hash to `require-directory` as the 2nd parameter (or 3rd if you're passing the path to another directory as the 2nd parameter already). Here are the available options:
89 Whitelisting (either via RegExp or function) allows you to specify that only certain files be loaded.
92 var requireDirectory = require('require-directory'),
93 whitelist = /onlyinclude.js$/,
94 hash = requireDirectory(module, {include: whitelist});
98 var requireDirectory = require('require-directory'),
99 check = function(path){
100 if(/onlyinclude.js$/.test(path)){
101 return true; // don't include
103 return false; // go ahead and include
106 hash = requireDirectory(module, {include: check});
111 Blacklisting (either via RegExp or function) allows you to specify that all but certain files should be loaded.
114 var requireDirectory = require('require-directory'),
115 blacklist = /dontinclude\.js$/,
116 hash = requireDirectory(module, {exclude: blacklist});
120 var requireDirectory = require('require-directory'),
121 check = function(path){
122 if(/dontinclude\.js$/.test(path)){
123 return false; // don't include
125 return true; // go ahead and include
128 hash = requireDirectory(module, {exclude: check});
131 ### Visiting Objects As They're Loaded
133 `require-directory` takes a function as the `visit` option that will be called for each module that is added to module.exports.
136 var requireDirectory = require('require-directory'),
137 visitor = function(obj) {
138 console.log(obj); // will be called for every module that is loaded
140 hash = requireDirectory(module, {visit: visitor});
143 The visitor can also transform the objects by returning a value:
146 var requireDirectory = require('require-directory'),
147 visitor = function(obj) {
148 return obj(new Date());
150 hash = requireDirectory(module, {visit: visitor});
156 var requireDirectory = require('require-directory'),
157 renamer = function(name) {
158 return name.toUpperCase();
160 hash = requireDirectory(module, {rename: renamer});
166 var requireDirectory = require('require-directory'),
167 hash = requireDirectory(module, {recurse: false});
179 [MIT License](http://www.opensource.org/licenses/mit-license.php)
183 [Troy Goode](https://github.com/TroyGoode) ([troygoode@gmail.com](mailto:troygoode@gmail.com))