objenv v3.1.0
OBJENV
objenv overrides an object with it's corresponding environment variables.
Install:
npm install objenv --saveQuick usage example:
// import objenv from 'objenv'
import {objEnv} from 'objenv'
const envEnrichedObject = objEnv(obj, {prefix: 'psichi'})objEnv(obj, options, matchFn)
Will traverse obj and test whether an environment variable is set to replace it's value.
matchFn(key, newValue) can be used to execute a function whenever a value is replaced.
This is can be useful to report about the environment override.
If you return false from matchFn the value will not be overwritten.
Available options are:
- options.prefix: defaults to none.
- options.separator: defaults to '_'
- options.camelCase: defaults to false
Both options and matchFn are optional, if options are omitted,
the matchFn will be the second argument.
objenv alters obj in place, if this is not desired create a copy of the object first.
The camelCase option will expand camelCased keys using the separator.
e.g. a key named databaseName can be overruled using a DATABASE_NAME environment variable.
Example Usage
Example object:
const config = {
database: {
host: 'localhost',
port: '8000'
}
};The initial object defines what values can be set.
According to the above the following environment variables will be considered:
DATABASE_HOST
DATABASE_PORTIf you provide a prefix option the environment variables must start with this
prefix. e.g. when the prefix is MYORG, the following env vars are tested:
MYORG_DATABASE_HOST
MYORG_DATABASE_PORTScript:
import objenv from 'objenv'
const options = {
prefix: 'myorg',
seperator: '_' // is the default
};
objenv(config, options, (key, value) => {
console.log('Env key %s found value is now %s', key, value)
});
console.log(config);Run:
$ MYORG_DATABASE_HOST=0.0.0.0 node script.js
{ "database": { "host": "0.0.0.0", "port": "8000"} }Similar modules:

