npm install dirreq
var dirreq = require('dirreq');
var requiredObject = dirreq(relativePath, options);
{
debug: Boolean, default false
defer: Boolean, default true
extensions: Array, default Object.keys(require.extensions)
onlyFiles: Boolean, default true
recursive: Boolean, default false
requireFunction: Function, default require
stripExtension: Boolean, default true
}
debug
: if set totrue
, will output debug information to stdout about skipped files and directoriesdefer
: if set totrue
, will call the require function only when accessed viarequiredDirectory.field
extensions
: accepted file extensions. By default,.js
,.json
and.node
are acceptedonlyFiles
: if set totrue
andrecursive
option is not set, will skip directories (i.e. not call the require function on anything that doesn't comply tofs.statSync(path).isFile()
)recursive
: if set totrue
, will also require directories inside the given base directoryrequireFunction
: if set, will call this function instead of the defaultrequire()
stripExtension
: if set totrue
, will remove extension in the field name in the returned object
test
|
+-+ test
|
+- nested.js
+- test1.js
+- test2.js
+- test3.txt
var dirreq = require('dirreq');
var dirs = dirreq('./test');
console.log(dirs);
/*
{ test: { nested: { nested: [Function] } },
test1: [Function],
test2: {} }
*/
var dirreq = require('dirreq');
var dirs = dirreq('./test', {
defer: true,
recursive: false,
});
console.log(dirs);
/*
{ test1: [Getter], test2: [Getter] }
*/
When loading files recursively (options.recurse
set to true
), the original directory structure will be kept.