Installations
npm install require-yml
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
10.15.3
NPM Version
6.4.1
Score
76.1
Supply Chain
100
Quality
75.8
Maintenance
100
Vulnerability
100
License
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Love this project? Help keep it running — sponsor us today! 🚀
Developer
cutsin
Download Statistics
Total Downloads
1,088,654
Last Day
60
Last Week
60
Last Month
12,431
Last Year
279,522
GitHub Statistics
7 Stars
26 Commits
3 Forks
2 Watching
8 Branches
5 Contributors
Bundle Size
171.94 kB
Minified
41.99 kB
Minified + Gzipped
Package Meta Information
Latest Version
2.0.0
Package Id
require-yml@2.0.0
Unpacked Size
15.07 kB
Size
5.46 kB
File Count
5
NPM Version
6.4.1
Node Version
10.15.3
Total Downloads
Cumulative downloads
Total Downloads
1,088,654
Last day
0%
60
Compared to previous day
Last week
-97.5%
60
Compared to previous week
Last month
-19.1%
12,431
Compared to previous month
Last year
93.1%
279,522
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
require-yml
Why
It's instead of require-yaml because of this reason.
And, it can require a yml/yaml/json file/whole directory, or with iterator, or use async callback.
Install
1npm install require-yml
Breaking changes in 2.0.0
- version
1.x
defaults to suppresses load/parse errors, version2.x
defaults to throw them. v1.4.x
andv2.x
let you provide your own error handlers: restore original behavior by providing your ownonLoadError
as an empty function.
Usage
configs directory:
1configs/ 2 |- foo/ 3 |- bar/ 4 |- a.yml 5 |- b.yaml 6 |- c.json 7 |- empty/
1const req = require('require-yml')
require a file (yml/yaml/json)
1const yml = req('./configs/foo/bar/a.yml') 2const yaml = req('./configs/foo/bar/b') // b.yaml 3const json = req('./configs/foo/bar/c.json') 4console.log(yml, yaml, json) 5// >> {}, {}, {}
require a directory
1const all = req('./configs') 2console.log(all) 3// >> json object {"foo":{"bar":[Object Object]}
- All files in the directory are required, as properties of an object.
- By default, the file base name as it appears on the OS is used as property name in the returned object.
an empty file/directory returns undefined
1const empty = req('./configs/empty') 2console.log(empty) 3// >> undefined
require an explicit list of files, the later merges into the former and cascades
1const yml = req(['./config/default.yml', './configs/local.yml'])
- having directories in these names will cause an error you should handle. We don't know of such use-case, but if you ever encounter one - you may provide your own loaders for the pattern
/\.yml$/
- see below)
require a list of files, the later cascades, but let the tool guess extensions
1const yml = req(['./config/default', './configs/local'])
Notes:
- by default, tool tries extensions by this order:
.js
,.yml
,.yaml
,.json
,/
(dir) All found are merged on each other, the later cascades. - the built-in
.js
first - gives you more power allowing to start with a type that is not native tojson
or safe-modeyaml
, e.g:1//file: config/strategies/cli-banner.js 2module.export = function CliBanner() { } 3 CliBanner.prototype.text = '@TITLE' 4CliBanner.prototype.header = function(title) { return this.text.replace(/@TITLE/, title) }
1#file: config/strategies/cli-banner.yaml 2CliBanner: 3prototype: 4 text: | 5 ----------------------- 6 | @TITLE | 7 -----------------------
require a list of files with unspecified endings, but you control what extensions to try and in what order
1const yml = req({ 2 targets: ['./config/default', './configs/local'], 3 extensions: [ '.json', '.yaml' ] 4})
- this results in try the load order below, where each stage treats it's previous as defaults and cascades it with it's own values, whenever such are found:
- file:
./config/default.json
- file:
./config/default.yaml
- directory:
./config/default/
- file:
./config/local.json
- file:
./config/local.yaml
- directory:
./config/local/
- file:
- Note: Mind the difference between loading a list of files and loading a directory:
- list of files - merges the later into the former, the later cascades.
- directory - uses by default file base-names as property names, where files of same name and different extensions are basically a list of files.
Provide your own logic to map files to property names
1const path = require('path') 2const camelCase = require('lodash/camelCase') 3const yml = req({ 4 target: './config', 5 fileToProp: file => camelCase(path.baseName(file)) 6})
file
provided tofileToProp
is a full absolute path as it appears on your OS- what
fileToProp(file)
returns is used as property name - if there is already a value there - it is merged into and cascaded by the current.
- Note:
targets
is a synonym fortarget
for readability . Each can be provided as a string or as an array of strings. If you provide both -target
is used,targets
is ignored. When it's provided as a string - it's understood as a list of files with a single-element.
Provide your own custom loaders
1const fs = require('fs') 2const jsonc = require('jsonc') 3const yml = req({ 4 targets: ['./config/default', './configs/local'], 5 loaders: [{ 6 pattern: /.jsonc?$/, //<-- this will match .json and .jsonc alike 7 load: target => jsonc.parse(fs.readFileSync(target)), 8 }]
Notes:
- user loaders precede built-in ones. Loader of first matched pattern is used.
The built-in loaders are:
{ pattern: /\.(yml|yaml)$/, load: target => jsYaml.load(fs.readFileSync(resolvePath(target), 'utf8')) }, { pattern: /\.(json|js)$/, load: target => require(resolvePath(target)) },
- order of
loaders
does not effect order of loaded files (order ofextensions
does, and only between files in same directory) - You can support custom extensions by providing
loaders
- You can have the tool try your custom extensions for paths you provide without extension by including it in
extensions
apply a custom mapper to each loaded file
1const mapper = function(json) { 2 json.inject = 'everywhere' 3 return json 4} 5// v >= 2.0 6const yml2 = req({ target: './configs', mapper }) 7console.log(yml2.foo.bar.a.inject) 8// >> 'everywhere' 9 10// legacy form (supported for backward compatibility) 11const yml1 = req('./configs', mapper) 12console.log(yml1.foo.bar.a.inject) 13
- mapper iterator is called for every value that is loaded before being added to the value tree.
- use mappers to map or mutate loaded values.
- suppress loaded values by returning a falsy value.
handle require or mapper errors
1const yml = req({ 2 target: './configs', 3 mapper: function broken(json) { 4 a = b // -> throws `a is undefined` 5 }, 6 onLoadError: err => { 7 // handle your errors here 8 switch(e.CODE) { 9 ... 10 } 11 }, 12})
or use the global hook:
1req.onLoadError = function(err) { 2 // handle your errors here 3 switch(e.CODE) { 4 ... 5 } 6}
async require
1req('./configs', null, function(yml){ 2 console.log(yml.foo.bar.a) 3}) 4// >> {}
Note: operation is pseudo async. Nothing happens in parallel, but the loading happens on next tick after your code has ran and all your declarations are made.
Test
1npm test
Test outputs numbered test cases. Numbered test-cases can be used to filter ran tests.
1node test 15,18
will run only cases 15,18.
License
![Empty State](/_next/static/media/empty.e5fae2e5.png)
No vulnerabilities found.
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
Found 6/24 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
project is not fuzzed
Details
- Warn: no fuzzer integrations found
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
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
20 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-v88g-cgmw-v5xw
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-896r-f27r-55mw
- 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-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-vh95-rmgr-6w4m
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9
- Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-c4w7-xm78-47vh
- Warn: Project is vulnerable to: GHSA-p9pc-299p-vxgp
Score
2
/10
Last Scanned on 2025-02-03
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-yml
yml-register
Hooks into require / import to load YAML files
require-yaml
require('require-yaml') lets you load YAML/YML files using require syntax. For example: var config = require('./config.yaml');
@js-x/require-yml
require('*.yml')
require-yamljs
Allows you to require *.yml files using the yamljs module