config-pnp
v1.1.2
Published
Plug & play configuration from .json files and ENV
Downloads
13
Readme
Plug-and-play configuration from .json files and ENV variables.
Example
// config/config.json or {env}.json
{
"database": {
"address": {
"host": "foo.com",
"port": 3306
},
// ...
}
}
DATABASE_ADDRESS_HOST=bar.com
MIGRATIONS_ENABLED=true
MAX_CONNECTIONS=100
const config = require('config-pnp')
// init() is optional: the library will silently initialize upon first configuration retrieval
config.init({
directory: './config', // or envvar NODE_CONFIG_DIR. Default: './config'. Absolute or relative
environment: 'dev' // or envvar NODE_ENV. Use null to disable
})
config.get('database.address.host') // "bar.com" - override from environment variable
config.get('database.address.port') // 3306
config.get('migrations.enabled') // true - parsed boolean
config.get('max_connections') // 100 - parsed integer
// or
config['database.address.host']
config['database.address.port']
config['migrations.enabled']
config['max_connections']
// or (not recommended: environment variables will be unavailable for nested objects)
config.database.address.host
config.database.address.port
config.migrations.enabled
config.max_connections
Note: configuration values are immutable.
Precedence
- config.json
{env}
.json- Environment variables