It is just helper for parsing configuration that you passing with environment variables to javascript object.
Use it with node.js version that support es6 specification
$ npm install @dormi/node-configenv --save
const ConfigEnv = require('@dormi/node-configenv');
const config = new ConfigEnv(configMap, options);
Format: {[envName]: [envOptions]}
Environment name for setting options
__
will be parsed as deep level
_
will be parsed as camel case
For example:
{
API__APP_PORT: 8090
}
Will be parsed as:
{
api: {
appPort: 8090
}
}
Options object for environment variable. If it not an object it will be used as default value.
required: false
- Check for setting environment variable, default: false
default: value
- Set default value if environment variable is undefined
global: true
- Set your config to global.config
. default: false
parse: true
- Parse strings to primitives. e.g "[1, 2, 3]"
will be parsed to array of numbers [1, 2, 3]
. default: false
envFile: true
- Load environment variables from file. If true it will search .env
file in project root. Can be string path. default: false
Create configuration:
const ConfigEnv = require('@dormi/node-configenv');
const config = new ConfigEnv({
PORT: {default: 8080},
MONGO__URL: {default: 'mongodb://localhost:27017/dbname'}
}, {global: true, parse: true});
Run node.js program with env variables:
PORT=9000 node index.js
global.config
will be:
{
port: 9000,
mongo: {
url: 'mongodb://localhost:27017/dbname'
}
}
Create configuration:
const ConfigEnv = require('@dormi/node-configenv');
const config = new ConfigEnv({
PORT: {default: 8080},
MONGO__URL: {default: 'mongodb://localhost:27017/dbname'}
}, {global: true, parse: true, envFile: true});
Create .env file in project root:
PORT=9000
MONGO__URL=mongodb://localhost:27020/some
global.config
will be:
{
port: 9000,
mongo: {
url: 'mongodb://localhost:27020/some'
}
}