envigorate
v1.0.2
Published
Envigorate is a simple module for populating configuration objects with environment variables.
Downloads
1
Readme
envigorate
Envigorate is a simple module for populating configuration objects with environment variables.
API
envigorate(config, env, strict): Record<string, unknown>
config
:Record<string, unknown>
- An object containing configuration information and possibly variable references.env
:Record<string, unknown>
- An optional object argument conntaining the variables to be inserted intoconfig
. Defaults toprocess.env
.strict
:boolean
- An optional argument denoting whether or not an error should be raised when a variable is referenced inconfig
that is not present inenv
.
Returns a copy of config
where values matching the format "${{ FOO }}" are replaced with values from env
. Optionally throws a EnvigorateMissingVariableError
if references are made to unknown variables.
EnvigorateMissingVariableError
Error thrown by envigorate()
when undefined variables are referenced in config
. Has a variables
property containing the names of the undefined variables.
Example
.env
FOOBAR=123
config.yml
test: ${{ FOOBAR }}
example.ts
import 'dotenv/config';
import { cleanEnv, num } from 'envalid';
import { envigorate } from 'envigorate';
import { readFileSync } from 'fs';
import { load } from 'js-yaml';
const config = envigorate(
load(readFileSync('config.yml').toString()) as Record<string, unknown>,
cleanEnv(process.env, {
FOOBAR: num()
})
);
console.log(config.test); // --> 123