Installations
npm install glob-module-file
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
18.2.0
NPM Version
8.1.4
Score
68.1
Supply Chain
97.6
Quality
73.8
Maintenance
100
Vulnerability
99.6
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Developer
TehShrike
Download Statistics
Total Downloads
64,107
Last Day
24
Last Week
114
Last Month
234
Last Year
3,001
GitHub Statistics
5 Stars
41 Commits
2 Forks
3 Watching
2 Branches
2 Contributors
Bundle Size
34.21 kB
Minified
11.46 kB
Minified + Gzipped
Package Meta Information
Latest Version
3.2.0
Package Id
glob-module-file@3.2.0
Unpacked Size
14.69 kB
Size
4.65 kB
File Count
12
NPM Version
8.1.4
Node Version
18.2.0
Publised On
14 Mar 2023
Total Downloads
Cumulative downloads
Total Downloads
64,107
Last day
-17.2%
24
Compared to previous day
Last week
70.1%
114
Compared to previous week
Last month
35.3%
234
Compared to previous month
Last year
50%
3,001
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
glob-module-file
Pass in a file pattern and an output path, and a file will be created that imports all the matching files, and then exports them as a single array.
Why would you need this?
It's really nice to be able to auto-initialize a whole bunch of parts of your application, without having to maintain a list of modules to require in your index.js:
1// Maintaining this is inconvenient 2const coolService = require('./service/cool/the-coolest.js') 3const niftyService = require('./service/nifty/nearly-as-cool.js') 4const yetAnotherService = require('./service/hope-you-didnt-forget-any-other-files.js') 5 6coolService(initialConfig) 7niftyService(initialConfig) 8yetAnotherService(initialConfig)
To avoid dealing with such ever-growing lists of modules, I've used this pattern in node.js code:
1glob.sync('service/**/*.js', { ignore: 'service/**/*.test.js' }) 2.map(file => require(`./${file}`)) 3.forEach(moduleFunction => { 4 moduleFunction(initialConfig) 5})
It finds all the js files in a part of the application's directory tree, require
s them, and then runs them as a function passing in some initial options.
However
The above code doesn't work for client-side apps built with a bundler.
Bundlers must be able to tell which files will be loaded by reading the code, without executing it.
So, you're back to maintaining a long list imports/requires somewhere. Unless you use this library.
What does it do?
Given these files:
fixtures/one.js
fixtures/someDirectory/two.js
Calling this library and passing in fixtures/**/*.js
and my-globbed-files.js
will cause it to spit out a file containing:
1const fixtures$47$one$46$js = require('./fixtures/one.js') 2const fixtures$47$someDirectory$47$two$46$js = require('./fixtures/someDirectory/two.js') 3 4module.exports = [ 5 fixtures$47$one$46$js, 6 fixtures$47$someDirectory$47$two$46$js, 7]
or, for es6 module bundlers:
1import fixtures$47$one$46$js from './fixtures/one.js' 2import fixtures$47$someDirectory$47$two$46$js from './fixtures/someDirectory/two.js' 3 4export default [ 5 fixtures$47$one$46$js, 6 fixtures$47$someDirectory$47$two$46$js, 7]
Then, all your index file needs to do is
1require('my-globbed-files.js').forEach(moduleFunction => { 2 moduleFunction(initialConfig) 3})
API
globModuleFile(options, [globOptions])
Returns a promise that resolves to the JS code with all the requires/imports and exports.
If an outputPath
is provided, the promise will not resolve until after the file is done being written.
options
pattern
: (required): Passed to glob.outputPath
: A file to be created or overwritten with all of the import code.exportWithPath
: If false, exports whatever value was imported from the original file. If true, exports an object{ path: thePathStringThatWasImported, export: theObjectExportedFromTheGlobbedFile }
. Defaults tofalse
.format
: Specify the module output - either the stringes
(import/export) orcjs
(require/module.exports). Defaults tocjs
sortFunction
: A comparison function to be passed toArray.sort
to determine the order to import the files in (and determine their order in the output array). Defaults to a function that orders by how deep in the directory structure the file is (shallowest to deepest).pathPrefix
: A string to be prepended to the paths passed torequire
. Defaults to./
importStar
: whether to useimport * as whateverExport
instead ofimport defaultExport
when outputting ES module syntax. Defaults tofalse
globOptions
Optional. Passed straight through as the options argument to glob
.
Example
1const expected = `const fixtures$47$one$46$js = require('./fixtures/one.js') 2const fixtures$47$someDirectory$47$two$46$js = require('./fixtures/someDirectory/two.js') 3 4module.exports = [ 5\tfixtures$47$one$46$js, 6\tfixtures$47$someDirectory$47$two$46$js, 7] 8` 9 10globModuleFiles({ pattern: 'fixtures/**/*.js', outputPath: '/tmp/globbed.js' }).then(code => { 11 code // => expected 12 13 require('fs').readFileSync('/tmp/globbed.js', { encoding: 'utf8' }) // => code 14}).catch(err => { 15 console.error(err) 16})
CLI
1glob-module-file --pattern="fixtures/**/*.js" --format=es --ignore="**/one.js"
Takes any number of named arguments corresponding to the options objects above.
Spits the code to stdout so you can pipe it into a file or what have you.
License
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 1/25 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
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
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
license file not detected
Details
- Warn: project does not have a license file
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 8 are checked with a SAST tool
Reason
26 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-ff7x-qrg7-qggm
- Warn: Project is vulnerable to: GHSA-ww39-953v-wcq6
- Warn: Project is vulnerable to: GHSA-pfrx-2q88-qq97
- Warn: Project is vulnerable to: GHSA-43f8-2h32-f4cj
- Warn: Project is vulnerable to: GHSA-rc47-6667-2j5j
- Warn: Project is vulnerable to: GHSA-qqgx-2p2h-9c37
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-p6mc-m468-83gw
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-5v2h-r2cx-5xgj
- Warn: Project is vulnerable to: GHSA-rrrm-qjm4-v8hf
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-px4h-xg32-q955
- Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-7p7h-4mm5-852v
- Warn: Project is vulnerable to: GHSA-38fc-wpqx-33j7
- Warn: Project is vulnerable to: GHSA-p9pc-299p-vxgp
Score
1.3
/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 glob-module-file
expandglob
Node.js module to expand glob file patterns.
resolve-dep
Return an array of resolved filepaths for require-able local or named npm modules. Wildcard (glob) patterns may be used.
glob-ext
Simple wrapper of glob module with multi file extension support
package-json-exports
A file based package.json exports generator depending on fast-glob