read-env
Convert environment variables into JSON object with parsed values.
Install
npm install --save read-env
Basic Example
Let's say you have some environment variables starting with prefix "EXAMPLE_" like below:
EXAMPLE_OBJECT_KEY= '{"prop": "value"}',
EXAMPLE_ARRAY_KEY= '[1,2,3, "string", {"prop": "value"}, 5.2]',
EXAMPLE_TRUE_KEY= 'true',
EXAMPLE_FALSE_KEY= 'false',
EXAMPLE_INT_KEY= '5',
EXAMPLE_FLOAT_KEY= '5.2',
EXAMPLE_STRING_KEY= 'example',
your-app.js
import readEnv from 'read-env';
const options = readEnv('EXAMPLE');
console.log(options);
Output:
{
arrayKey: [ 1, 2, 3, 'string', { prop: 'value' }, 5.2 ],
falseKey: false,
floatKey: 5.2,
intKey: 5,
objectKey: { prop: 'value' },
stringKey: 'example',
trueKey: true
}
Usage
readEnv(prefix = null, transformKey = 'camelcase')
You can pass a string prefix as first paremeter like below:
const options = readEnv('EXAMPLE');
const optionsLower = readEnv('EXAMPLE', 'lowercase');
function ucfirst(string) {
return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
}
const optionsUcfirst = readEnv('EXAMPLE', ucfirst);
readEnv(config)
You can pass whole config object:
function ucfirst(string) {
return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
}
const options = readEnv({
prefix: 'EXAMPLE',
includePrefix: false,
transformKey: ucfirst,
parse: {
array: false,
},
});
Config
Available Config Options:
prefix
(type: string, default: null): filters environment variables by prefixincludePrefix
(type: bool, default: false): set true if you want to keep prefix in property names.transformKey
(type: null|string|function, default: 'camelcase'): transform environment variable name.
null
, doesn't transform the environment variable name.camelcase
, transforms variable name to camelCase.lowercase
, transforms variable name to lowercase.uppercase
, transforms variable name to UPPERCASE.fn(varName)
, you can write your own transformer function (varName will be provided with prefix, if includePrefix is true)
parse
(type: bool|object, default: object):
false
: returns raw environment variable value{}
: allows you to define which value types are going to be parsed.
object
(type: bool, default: true): parse string as object (value must be valid JSON input, see: JSON.parse).array
(type: bool, default: true): parse stringified array (value must be valid JSON input, see: JSON.parse).int
(type: bool, default: true): parse digits into integer (value consists of only numbers).float
(type: bool, default: true): parse decimals into integer (value consists of only numbers with decimal point).bool
(type: bool, default: true): parse if string equals to 'true' or 'false'.
filter
(type: null|function, default: null): filters environment variables (overrides prefix rule).
null
, don't filter varaibles.fn(envVarName, index)
, custom filter function (envVarName will be provided without any transformation).
Use Case Example
Recently, I used Nightmare for acceptance testing and had several environments which have different configurations.
Instead of writing a code like below:
import Nightmare from 'nightmare';
const nightmare = Nightmare({
show: process.env.X_NIGHTMARE_SHOW || false,
width: process.env.X_NIGHTMARE_WIDTH || 1280,
height: process.env.X_NIGHTMARE_HEIGHT || 720,
typeInterval: process.env.X_NIGHTMARE_TYPE_INTERVAL || 50,
});
I wrote this, and nightmare is fully configurable with environment variables :)
import Nightmare from 'nightmare';
import readEnv from 'read-env';
const nightmareConfig = readEnv('X_NIGHTMARE');
const nightmare = Nightmare(nightmareConfig);
LICENCE
MIT (c) 2017 Mehmet Yatkı