Recursively iterates over specified directory, require()'ing each file, and returning a nested hash structure containing those modules.
Recursively iterates over specified directory,
require()'ing each file, and returning a nested hash structure containing those modules.
$ npm install require-directory
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:
require-directory to build the hash (rather than doing so manually) like so:
var requireDirectory = require'require-directory';moduleexports = requireDirectorymodule;
routes/index.js like any other module, but it now has a hash/tree of the exports from the
var routes = require'./routes';// snipappget'/' routeshome;appget'/register' routesauthregister;appget'/login' routesauthlogin;appget'/logout' routesauthlogout;
routes variable above is the equivalent of this:
var routes =home: require'routes/home.js'auth:login: require'routes/auth/login.js'logout: require'routes/auth/logout.js'register: require'routes/auth/register.js';
routes.index will be
undefined as you would hope.
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
var requireDirectory = require'require-directory';moduleexports = requireDirectorymodule './some/subdirectory';
For example, in the example in the Usage section we could have avoided creating
routes/index.js and instead changed the first lines of
var requireDirectory = require'require-directory';var routes = requireDirectorymodule './routes';
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:
Whitelisting (either via RegExp or function) allows you to specify that only certain files be loaded.
var requireDirectory = require'require-directory'whitelist = /onlyinclude.js$/hash = requireDirectorymodule include: whitelist;
var requireDirectory = require'require-directory'if/onlyinclude.js$/testpathreturn true; // don't includeelsereturn false; // go ahead and includehash = requireDirectorymodule include: check;
Blacklisting (either via RegExp or function) allows you to specify that all but certain files should be loaded.
var requireDirectory = require'require-directory'blacklist = /dontinclude\.js$/hash = requireDirectorymodule exclude: blacklist;
var requireDirectory = require'require-directory'if/dontinclude\.js$/testpathreturn false; // don't includeelsereturn true; // go ahead and includehash = requireDirectorymodule exclude: check;
require-directory takes a function as the
visit option that will be called for each module that is added to module.exports.
var requireDirectory = require'require-directory'console.logobj; // will be called for every module that is loadedhash = requireDirectorymodule visit: visitor;
The visitor can also transform the objects by returning a value:
var requireDirectory = require'require-directory'return obj;hash = requireDirectorymodule visit: visitor;
var requireDirectory = require'require-directory'return nametoUpperCase;hash = requireDirectorymodule rename: renamer;
var requireDirectory = require'require-directory'hash = requireDirectorymodule recurse: false;
$ npm run lint$ npm test