Gathering detailed insights and metrics for require-directory
Gathering detailed insights and metrics for require-directory
Gathering detailed insights and metrics for require-directory
Gathering detailed insights and metrics for require-directory
@types/require-directory
TypeScript definitions for require-directory
import-cwd
Import a module like with `require()` but from the current working directory
resolve-cwd
Resolve the path of a module like `require.resolve()` but from the current working directory
require-at
Call require pretending your are at another directory
Recursively iterates over specified directory, requiring each file, and returning a nested hash structure containing those libraries.
npm install require-directory
Typescript
Module System
Min. Node Version
Node Version
NPM Version
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
412 Stars
79 Commits
30 Forks
5 Watchers
1 Branches
9 Contributors
Updated on Apr 16, 2025
Latest Version
2.1.1
Package Id
require-directory@2.1.1
Size
4.27 kB
NPM Version
2.5.1
Node Version
0.12.0
Published on
May 28, 2015
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
Recursively iterates over specified directory, require()
'ing each file, and returning a nested hash structure containing those modules.
Follow me (@troygoode) on Twitter!
1$ 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:
routes/index.js
uses require-directory
to build the hash (rather than doing so manually) like so:
1var requireDirectory = require('require-directory'); 2module.exports = requireDirectory(module);
app.js
references routes/index.js
like any other module, but it now has a hash/tree of the exports from the ./routes/
directory:
1var routes = require('./routes'); 2 3// snip 4 5app.get('/', routes.home); 6app.get('/register', routes.auth.register); 7app.get('/login', routes.auth.login); 8app.get('/logout', routes.auth.logout);
The routes
variable above is the equivalent of this:
1var routes = { 2 home: require('routes/home.js'), 3 auth: { 4 login: require('routes/auth/login.js'), 5 logout: require('routes/auth/logout.js'), 6 register: require('routes/auth/register.js') 7 } 8};
Note that 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 requireDirectory(module, __dirname)
:
1var requireDirectory = require('require-directory'); 2module.exports = requireDirectory(module, './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 app.js
to:
1var requireDirectory = require('require-directory'); 2var routes = requireDirectory(module, './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.
1var requireDirectory = require('require-directory'), 2 whitelist = /onlyinclude.js$/, 3 hash = requireDirectory(module, {include: whitelist});
1var requireDirectory = require('require-directory'), 2 check = function(path){ 3 if(/onlyinclude.js$/.test(path)){ 4 return true; // don't include 5 }else{ 6 return false; // go ahead and include 7 } 8 }, 9 hash = requireDirectory(module, {include: check});
Blacklisting (either via RegExp or function) allows you to specify that all but certain files should be loaded.
1var requireDirectory = require('require-directory'), 2 blacklist = /dontinclude\.js$/, 3 hash = requireDirectory(module, {exclude: blacklist});
1var requireDirectory = require('require-directory'), 2 check = function(path){ 3 if(/dontinclude\.js$/.test(path)){ 4 return false; // don't include 5 }else{ 6 return true; // go ahead and include 7 } 8 }, 9 hash = requireDirectory(module, {exclude: check});
require-directory
takes a function as the visit
option that will be called for each module that is added to module.exports.
1var requireDirectory = require('require-directory'), 2 visitor = function(obj) { 3 console.log(obj); // will be called for every module that is loaded 4 }, 5 hash = requireDirectory(module, {visit: visitor});
The visitor can also transform the objects by returning a value:
1var requireDirectory = require('require-directory'), 2 visitor = function(obj) { 3 return obj(new Date()); 4 }, 5 hash = requireDirectory(module, {visit: visitor});
1var requireDirectory = require('require-directory'), 2 renamer = function(name) { 3 return name.toUpperCase(); 4 }, 5 hash = requireDirectory(module, {rename: renamer});
1var requireDirectory = require('require-directory'), 2 hash = requireDirectory(module, {recurse: false});
1$ npm run lint 2$ npm test
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
0 existing vulnerabilities detected
Reason
Found 6/27 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More