Open ENV
Use some simple conventions to read process.env into a deeply nested javascript object recognizing
arrays and numbers.
Install
yarn add openenv
or npm install openenv
Basic Usage
{
'NODE_ENV':'development',
'socket.port':'1234',
'socket.host':'localhost',
'express.port':'4321'
'express.host':'localhost',
'whitelist':'http://example1.com,http://example2.com'
}
const config = require('openenv')(process.env)
{
NODE_ENV:'development',
socket:{
port:1234,
host:'localhost',
},
express:{
port:4321,
host:'localhost',
},
whitelist:['http://example1.com','http://example2.com']
}
Justification
Its is sometimes necessary for a configuration object to be more complex than what can be represented
in a standard process.env of string:string mappings. This library defines a convention to define keys
which map to nested paths in an object, as well as rules for parsing arrays and numbers from values.
Advanced configuration allows you to prefix or regex filter unecessary variables from your
final configuation object. If you can follow these conventions in your env then you dont need
any specific env parsing logic in your application, simplifying your app configuration.
There are many other libraries like this available, the main difference with this one is that you
can configure your delimiters (ex: use '_' instead of '.' for paths), as well as Regex key filtering
(ex: only parse envs starting with lowercase), map values and other options.
API
openenv(process.env,options) => config
const openenv = require('openenv')
const config = openenv(process.env,{
regex:undefined,
prefix:'',
arrayDelimiter:','
pathDelimiter:'.'
valueParser:x=>JSON.parse(x)
keyParser:x=>x.replace(prefix,'')
})