Unify config from `package.json`, `.env` file, environment variables and argv
BSD-3-CLAUSE License
Unify config from package.json
(both caller modules and project root), .env
files, environment variables and CLI arguments.
npm install unify-config
config()
will pick your config from the config
field on your package.json
file, your environment variables and your CLI arguments, and you can also set a
parsers
option to parse .env
files and what extensions they should be used
for each one. You can also define aliases
, for example to map environment
variables to your app arguments. Your config will return parsed as Javascript
objects by using string2js module.
process.argv.slice(2)
process.env
JSON.parse()
canparsers
value or toJSON.parse()
using files with .json
extension.const unifyConfig = require('unify-config')
const {parse: dotenv} = require('dotenv')
const {parse: ini} = require('ini')
const json5 = require('json5/lib/parse')
const {load: yaml} = require('js-yaml/lib/js-yaml/loader')
const {parse: toml} = require('toml')
const config = unifyConfig({
aliases: {
GITHUB_REPOSITORY: function(value)
{
const [user, repo] = value.split('/')
return {repo, user}
},
GITHUB_TOKEN: 'auth'
},
parsers: [
[dotenv, ''], // `dotenv` don't have an extension
[ini , '.ini'],
[json5 , '.json', '.json5'],
[toml , '.toml'],
[yaml , '.yaml', '.yml']
]
})
Like config()
, but also sets the parsed values to the provided env
object
(or process.env
if none is provided). Options are the same of config()
.
.env
files parsers