
Research
/Security News
9 Malicious NuGet Packages Deliver Time-Delayed Destructive Payloads
Socket researchers discovered nine malicious NuGet packages that use time-delayed payloads to crash applications and corrupt industrial control systems.
glob-require-dir
Advanced tools
Node helper module to require() complete directories.
Allows you to traverse through directories and require() every file that can be require()'d. All require()'d files are returned as hash with customizable keys. A minimatch pattern can be provided as well as node-glob options.
This module was inspired by require-dir - a great module to require directories but mostly without options to modify the outcome of the require process.
const { foo, bar } = require('baz'); and even import { foo, bar } from './baz')to returnmy-moduleasmyModule`)export defaultnode-glob with support for node-glob options and glob patternsnpm install glob-require-dir
Given this directory structure:
Controllers/
├── League.js
├── Player.js
└── Team.js
require('glob-require-dir')({cwd: './Controllers'}) will return the equivalent of:
{
League: require('./Controllers/League.js'),
Player: require('./Controllers/Player.js'),
Team: require('./Controllers/Team.js')
}
The easiest and recommended way to use glob-require-dir is to create an index.js within your desired directory and add the following line:
module.exports = require('glob-require-dir')();
You can then use const Controllers = require('./Controllers'); to have all your controllers in your Controllers variable. Doing it this way you can also use ES2015 object destructuring:
const { League, Player } = reqiure('./Controllers');
or as ES module:
import { League, Player } from './Controllers';
For more examples have a look at the ./tests folder.
Require the module itself:
const globRequireDir = require('glob-require-dir');
it will export a function which expects 0, 1 or 2 arguments.
globRequireDir()
If no argument is given, all files from the current working dir will be loaded.
globRequireDir('Controllers/')
If only one argument is specified and the argument is a string, it will be treated as minimatch pattern.
globRequireDir({ cwd: './Controllers' })
If only one argument is specified and the argument is an object, it will be treated as options object.
globRequireDir('Controllers/', { transform: ['uppercase'] })
If two arguments are given, the first argument will be used as minimatch pattern while the second argument will be used as options object.
glob-require-dir supports a number of options. You can pass them as single or as second parameter to the globRequireDir() function.
Default:: ['camelcase', 'basename']
glob-require-dir uses transformer functions (or short: transforms) when creating the keys for the exported hash. By default the full path of a matching file will be camelCased and then basenamed (in other words: the file extension will be dropped). There are a number of supported transforms (UPPERCASE, lowercase, camelCase, snake_case, …) but you can also deactivate it completely or write your own custom transformer functions. Custom transformer functions take the current filename as only argument and must return the transformed key as string.
globRequireDir({
transform: [
'basename', // get the basename of a matching file first
(key) => key.replace(/1/g,'one') // then replace every "1" in all filenames with "one"
]
});
Attention: the order in which the transformer functions are specified matters! While ['basename', 'camelcase'] gives you MyExample on a file ./Controllers/my-example, ['camelcase', 'basename'] gives you ControllersMyExample.
Default: false
The recursive option prefixes your pattern (no matter if a custom pattern is given or not) with **/ so that glob loads files from a folder as well as from subfolders.
Default: ''
Appends a string to the transformed key.
globRequireDir({
cwd: './Controllers',
append: 'Controller'
});
would yield an object like:
{
LeagueContoller: […],
PlayerContoller: […],
TeamContoller: […]
}
Default: ''
Same as append, but prepends …
Default: false
Enabling this option makes glob-require-dir to look for a property default in the export of the required file. If a default export is found, this will be used as only export.
Default: null
All glob options with the exception of cwd (which is used internally) and nodir (which is always false) will be passed 1:1 to the glob.sync() call.
Default: ['**/node_modules**']
Shortcut to globOptions.ignore for convenience. You can specify a pattern or an array of patterns which will be ignored while loading files.
FAQs
Node helper module to `require()` complete directories.
We found that glob-require-dir demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Research
/Security News
Socket researchers discovered nine malicious NuGet packages that use time-delayed payloads to crash applications and corrupt industrial control systems.

Security News
Socket CTO Ahmad Nassri discusses why supply chain attacks now target developer machines and what AI means for the future of enterprise security.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.