Installations
npm install require-glob
Developer Guide
Typescript
No
Module System
CommonJS
Min. Node Version
>= 10
Node Version
16.13.2
NPM Version
8.1.2
Score
93
Supply Chain
100
Quality
75.3
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Developer
Download Statistics
Total Downloads
7,229,301
Last Day
3,690
Last Week
15,819
Last Month
70,048
Last Year
1,122,037
GitHub Statistics
21 Stars
87 Commits
3 Watching
2 Branches
3 Contributors
Bundle Size
86.44 kB
Minified
26.42 kB
Minified + Gzipped
Package Meta Information
Latest Version
4.1.0
Package Id
require-glob@4.1.0
Unpacked Size
18.80 kB
Size
6.13 kB
File Count
20
NPM Version
8.1.2
Node Version
16.13.2
Total Downloads
Cumulative downloads
Total Downloads
7,229,301
Last day
8.2%
3,690
Compared to previous day
Last week
-12.7%
15,819
Compared to previous week
Last month
11.4%
70,048
Compared to previous month
Last year
2.2%
1,122,037
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
3
require-glob
Requires multiple modules using glob patterns and combines them into a nested object.
Install
$ npm install --save require-glob
Usage
┣━ unicorn.js
┣━ cake.js
┗━ rainbow/
┣━ red-orange.js
┣━ _yellow_green.js
┗━ BluePurple.js
1var requireGlob = require('require-glob'); 2 3requireGlob(['**/*.js', '!cake.js']).then(function (modules) { 4 console.log(modules); 5 // { 6 // unicorn: [object Object], 7 // rainbow: { 8 // redOrange: [object Object], 9 // _yellow_green: [object Object], 10 // BluePurple: [object Object] 11 // } 12 // } 13});
API
requireGlob(patterns [, options]): Promise
Returns a promise that resolves to an object containing the required contents of matching globbed files.
requireGlob.sync(patterns [, options]): Object
Returns an object containing the required contents of matching globbed files.
patterns
Type: {String|Array.<String>}
One or more minimatch
glob patterns patterns. Supports negation.
options
Type: {Object}
(optional)
This object is ultimately passed directly to node-glob
so check there for more options, in addition to those below.
cwd
Type: {String}
(default: __dirname
)
The current working directory in which to search. Defaults to the __dirname
of the requiring module so relative paths work the same as Node.js's require.
base
Type: {String}
(default: common non-glob parent)
Default is everything before the first glob starts in the first pattern (see glob-parent
).
This option has no effect if you define your own mapper
function.
1requireGlob(['./src/**', './lib/**'], { cwd: '/home/jdoe/my-module' });
2// base is: /home/jdoe/my-module/src
3
4requireGlob('./{src,lib}/**', { cwd: '/home/jdoe/my-module' });
5// base is: /home/jdoe/my-module
bustCache
Type: {Boolean}
(default: false
)
Whether to force the reload of modules by deleting them from the cache. Useful inside watch tasks.
This option has no effect if you define your own mapper
function.
mapper
Type: {Function(options, filePath, i, filePaths) : Object}
The mapper is reponsible for requiring the globbed modules. The default mapper returns an object containing path information and the result of requiring the module.
1// file: /home/jdoe/my-module/index.js 2requireGlob('./src/**/*.js'); 3 4// the resulting list of files 5[ 6 './src/unicorn.js', 7 './src/rainbow/red-orange.js', 8 './src/rainbow/_yellow_green.js', 9 './src/rainbow/BluePurple.js', 10] 11 12// will be mapped to 13[ 14 { 15 cwd: '/home/jdoe/my-module', 16 base: '/home/jdoe/my-module/src', 17 path: '/home/jdoe/my-module/src/unicorn.js', 18 exports: require('./src/unicorn') 19 }, 20 { 21 cwd: '/home/jdoe/my-module', 22 base: '/home/jdoe/my-module/src', 23 path: '/home/jdoe/my-module/src/rainbow/red-orange.js', 24 exports: require('./src/rainbow/red-orange') 25 }, 26 { 27 cwd: '/home/jdoe/my-module', 28 base: '/home/jdoe/my-module/src', 29 path: '/home/jdoe/my-module/src/rainbow/_yellow_green.js', 30 exports: require('./src/rainbow/_yellow_green') 31 }, 32 { 33 cwd: '/home/jdoe/my-module', 34 base: '/home/jdoe/my-module/src', 35 path: '/home/jdoe/my-module/src/rainbow/BluePurple.js', 36 exports: require('./src/rainbow/BluePurple') 37 } 38]
reducer
Type: {Function(options, result, fileObject, i, fileObjects): Object}
The reducer is responsible for generating the final object structure. The default reducer expects an array as produced by the default mapper and turns it into a nested object. Path separators determine object nesting. Directory names and file names are converted to camelCase
. File extensions are ignored.
1// mapper example is reduced to 2 3{ 4 unicorn: require('./src/unicorn.js'), 5 rainbow: { 6 redOrange: require('./src/rainbow/red-orange.js'), 7 _yellow_green: require('./src/rainbow/_yellow_green.js'), 8 BluePurple: require('./src/rainbow/BluePurple.js'), 9 } 10}
initialValue
Type: {any}
(default: {}
)
The initial value passed to the reducer. The default is an empty object, as expected by the default reducer.
1// file: /home/jdoe/my-module/index.js 2const defaultDependencies = { 3 clover: require('clover'), 4 unicorn: require('unicorn'), 5}; 6 7requireGlob('./src/**/*.js', { 8 initialValue: defaultDependencies, 9}); 10 11// reducer example is changed to 12{ 13 clover: require('clover'), 14 unicorn: require('./src/unicorn.js'), 15 rainbow: { 16 redOrange: require('./src/rainbow/red-orange.js'), 17 _yellow_green: require('./src/rainbow/_yellow_green.js'), 18 BluePurple: require('./src/rainbow/BluePurple.js'), 19 } 20}
keygen
Type: {Function(options, fileObj): String|Array.<String>}
The default reducer uses this function to generate a unique key path for every module. The default keygen converts hyphenated and dot-separated sections of directory names and the file name to camelCase
. File extensions are ignored. Path separators determine object nesting.
This option has no effect if you define your own reducer
function.
1// given the mapped object 2{ 3 cwd: '/home/jdoe/my-module', 4 base: '/home/jdoe/my-module/src', 5 path: '/home/jdoe/my-module/src/fooBar/bar-baz/_bat.qux.js', 6 exports: require('./src/fooBar/bar-baz/_bat.qux.js') 7} 8 9// the keygen will produce 10[ 11 'fooBar', 12 'barBaz', 13 '_batQux' 14] 15 16// which the reducer will use to construct 17{ 18 fooBar: { 19 barBaz: { 20 _batQux: require('./src/fooBar/bar-baz/_bat.qux.js') 21 } 22 } 23}
Contribute
Standards for this project, including tests, code coverage, and semantics are enforced with a build tool. Pull requests must include passing tests with 100% code coverage and no linting errors.
Test
$ npm test
MIT © Shannon Moeller
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: license:0
- Info: FSF or OSI recognized license: MIT License: license:0
Reason
3 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-cj88-88mr-972w
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
Reason
Found 3/20 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/main.yml:1
- Info: no jobLevel write permissions found
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:22: update your workflow using https://app.stepsecurity.io/secureworkflow/shannonmoeller/require-glob/main.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:23: update your workflow using https://app.stepsecurity.io/secureworkflow/shannonmoeller/require-glob/main.yml/master?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/main.yml:27
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 npmCommand dependencies pinned
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 14 are checked with a SAST tool
Score
3.2
/10
Last Scanned on 2025-01-27
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 MoreOther packages similar to require-glob
global
Require global variables
babel-plugin-require-glob
Babel plugin which gives you ability to require globs.
require-glob-array
`require` node modules and place their exported values in an array
glob-require
Use glob to find and call `require` on all matching files in a directory tree.