Package Exports
- require-dir-all
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (require-dir-all) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
require-dir-all
Yet another Node.js helper to require
all files in directory.
Useful when needed to require
group of files in same directory(-ies) with similar functionality,
like routes, controllers, middlewares, models, etc.
Inspired by require-all and
require-dir packages.
Both of them are good, but the first of them lacks relative paths support (need to use __dirname
) and always recursive,
while the second one lacks file/dir filtering and for some reason store modules in non-hierarchical structure, taking only
one file from several ones with the same name.
Installation
npm install require-dir-all --save
Usage
Basic usage
var modules = require('require-dir-all')('directory_to_require');
Now variable modules
will contain exported values
from all the modules .js
, .json
, .coffee
in directory
accessible by its properties, for example modules.module1
as if they were require'd like:
modules = {
module1: require('module1')
module2: require('module2')
}
If you need more than one directory to require
, you can provide array of directories:
var modules = require('require-dir-all')(['dir1', dir2]);
Variable modules
will be array of objects with module's exports, equivalent to:
modules = [
{ module1: require('module1') },
{ module2: require('module2') }
]
Options
You may provide additional options using second optional parameter:
var modules = require('require-dir-all')(
'directory_to_require', // relative or absolute directory
{ // options
map: function(reqModule) { /* you may postprocess the name of property the module will be stored and exported object */ }
recursive: false, // recursively go through subdirectories; default value shown
includeFiles: /^.*\.(js|json|coffee)$/, // RegExp to select files; default value shown
excludeDirs: /^(\.git|\.svn|node_modules)$/ // RegExp to ignore subdirectories; default value shown
}
);
Options:
map
: function to postprocess eachrequire
'd file (see example below); default:null
recursive
- recursively go through subdirectories; default:false
includeFiles
- reg exp to include files, default:/^.*\.(js|json|coffee)$/
, which means torequire
only.js
,.json
,.coffee
filesexcludeDirs
- reg exp to exclude subdirectories (whenrecursive: true
), default:/^(\.(git|svn)|(node_modules))$/
, which means to exclude directories.git
,.svn
,node_modules
while going recursively
Tips
Typical task is to run the function for each module required from the directory (like init or shutdown routines). With this module it is needed to reqursively go through all the properties (i.e.module's exports) and run the function for each of them
If you need to wait until the end of initialization of all the modules, using async
(assuming each module's initialize method accepts callback as a parameter).
Please, be aware, that the methods below applicable only
Require'd files modules/module1.js
and modules/module2.js
var path = require('path'),
fileExt = path.extname(module.filename),
fileBase = path.basename(module.filename, fileExt);
module.exports = {
initialize: function(cb) {
console.log('module ' + fileBase + ' initialized');
return cb(false, 'result from '+fileBase);
}
};
Require'ing file index.js
:
var _ = require('lodash');
var async = require('async');
var modules = require('require-dir-all')('modules');
var initialize = function(callback) {
var initializers = [];
_.forOwn(modules, function(module) {
initializers.push( function(cb) { return module.initialize(cb); } );
});
async.parallel(initializers, callback);
};
initialize(function(err, results) {
console.log('initialize done; results:', results);
});
/*
Output:
module module1 initialized
module module2 initialized
initialize done; results: [ 'result from module1', 'result from module2' ]
*/
If you do not need to wait till the finish of initialization of both modules:
var _ = require('lodash');
var modules = require('require-dir-all')('modules');
module.exports.initialize = function() {
_.forOwn(modules, function(module) {
// for (var module in modules) { if (modules.hasOwnProperty(module)) {
module.initialize(); ;
// } }
});
};
See demo/initializers
for an example
Simple
If you need to require all the .js
, .json
, .coffee
files in the directory modules
, add following line:
var modules = require('require-dir-all')('modules');
or if you like:
var require_dir_all = require('require-dir-all');
var modules = require_dir_all('modules');
Object modules
will be populated with properties which names will correspond to module names and values - to exported
objects.
Traditional equivalent:
modules = {
module1: require('module1')
module2: require('module2')
}
By default directories .git
, .svn
, node_modules
are excluded.
Example
Assume you have following structure:
modules/
module1.js
module2.js
app.js
File module1.js
exports:
module.exports = 'string exported from module 1';
File module2.js
exports:
module.exports = 'string exported from module 2';
In app.js
:
var modules = require('require-dir-all')('modules');
console.log('modules:', modules);
Result:
modules: {
module1: 'string exported from module 1',
module2: 'string exported from module 2'
}
You can find this example in demo/simple/
To run it:
cd demo/simple/
npm install
node app
Recursive
Option recursive: true
allows to require recursively the directory and all its subdirectories.
Example
You can find this example in demo/recursive/
Directory structure:
$ ls -R demo/recursive/modules/
demo/recursive/modules/:
dir1 dir.a.b.c excluded excluded.2 module1.js module2.js
demo/recursive/modules/dir1:
dir2 module3.js
demo/recursive/modules/dir1/dir2:
module4.js
demo/recursive/modules/dir.a.b.c:
module5.js
demo/recursive/modules/excluded:
excluded.js
demo/recursive/modules/excluded.2:
excluded.js
File app.js:
'use strict';
var modules = require('require-dir-all')(
'modules', {
recursive: true,
excludeDirs: /^excluded.*$/
}
);
console.log('modules:', JSON.stringify(modules, null, 2));
Output:
modules: {
"dir.a.b.c": {
"module5": "string exported from module 5"
},
"dir1": {
"dir2": {
"module4": "string exported from module 4"
},
"module3": "string exported from module 3"
},
"module1": "string exported from module 1",
"module2": "string exported from module 2"
}
Map
Option map
allows to define function to run for each require
'd file.
Object properties. These properties may be changed:
name
- module name to be stored in result objectexports
- module's exports value
These properties are read-only:
path
- filepath,base
- base part of file name,ext
- file extension
Assume you have following structure:
modules/
module1
module2
If each file module1.js
, module2.js
in modules
directory exports a constructor
to which the some config parameters are passed like this:
'use strict';
// Object constructor
var Object1 = function(config) {
this.name = 'Object1';
this.config = config;
};
// Exporting constructor function
module.exports = Object1;
and the code which require
's these files in app_old.js
is like following:
// For
var config1 = { value: 'config1' },
config2 = 'config2';
var module1 = new (require('modules/module1'))(config1),
module2 = new ()require('module/module2'))(config2);
You may replace this with following code:
// Store config for each module in config object properties
// with property names corresponding to module names
var config = {
module1: { value: 'config1' },
module2: { value: 'config2' }
};
// Require all files in modules subdirectory
var modules = require('require-dir-all')(
'modules', // Directory to require
{ // Options
map: function(reqModule) {
// define function to be post-processed over exported object for each require-d module
reqModule.exports =
// create new object with corresponding config passed to constructor
new reqModule.exports( config[reqModule.name] );
// Also may change the property name if needed
// reqModule.name = 'prefix_'+reqModule.name;
}
}
);
console.log('modules:', JSON.stringify(modules, null, 2));
Result:
modules: {
"module1": {
"name": "Object1",
"config": {
"value": "config1"
}
},
"module2": {
"name": "Object2",
"config": {
"value": "config2"
}
}
}
You can find this example in demo/map/
To run it:
cd demo/map/
npm install
node app
TODO
- Add support for glob-like definitions?
// from http://stackoverflow.com/a/28976201/2774010
var glob = require( 'glob' )
, path = require( 'path' );
glob.sync( './routes/**/*.js' ).forEach( function( file ) {
require( path.resolve( file ) );
});
Add support for array of dirs as the first parameter
Restructure README.md to make it more readable (as an example -
browserify
)