good-env


🚨 v7 requires Node version 18.20.4 or higher! 🚨
good-env provides a more intuitive way to interface with environment variables for node apps. Reasoning
about raw strings is OK for some things but for non-trivial applications, booleans, numbers, lists or even
the existence (or non-existence) of environment configurations can play a key role in how an application behaves. Lastly, good-env has no production dependencies.
$ npm install good-env --save
With normal process.env
$ export HOST=localhost
$ export SECRET=shhh
$ export FOO=10
$ export A_TRUE_VAL=true
$ export A_FALSE_VAL=false
$ export LIST=foo,bar,bang
$ export ENDPOINT=https://foo.com
$ node
> process.env.FOO
'10'
> process.env.A_TRUE_VAL
'true'
> process.env.A_FALSE_VAL
'false'
> process.env.LIST
'foo,bar,bang'
>
Using good-env
const env = require('good-env')
env.getNumber('FOO')
env.getBool('A_TRUE_VAL')
env.getBool('A_FALSE_VAL')
Warning
Checking the existence of a boolean value which resolves to false will return true because ok() doesn't give you a value.
export A_BOOL_VAL=false
env.ok('A_BOOL_VAL')
Specify defaults
env.get('NOT_SET', 'foo')
Batch Gets
env.getAll(['SECRET', 'HOST'])
env.getAll({
A_SECRET: 'lolz',
HOST: null
})
Use the first available environment variable
const host = process.env.THE_HOST || process.env.HOST
const host = env.get(['THE_HOST', 'HOST'])
const host = env.get(['THE_HOST', 'A_HOST'], 'localhost')
Lists
env.getList('LIST')
env.getList('LIST_NOT_SET')
Number Lists
$ export LIST=1,2,3
process.env.LIST
env.list('LIST', { cast: 'number' })
Sometimes you just need to know if something exists
env.ok('NOT_SET')
env.ok('FOO')
env.ok('FOO', 'BAR')
env.ok('FOO', 'BAR', 'NOT_SET')
Use .assert(item1, item2...) to check the existence and/or type of a few items at once
Note: If any variable passed to assert() doesn't exist or is otherwise
invalid, an error will be thrown.
env.assert(
'HOSTNAME',
{ 'PORT': { type: 'number' }},
{ 'INTERVAL': { type: 'number', ok: s => s >= 1000 }}
)
Fetch AWS Credentials
const {
awsKeyId,
awsSecretAccessKey,
awsRegion,
} = env.getAWS();
const {
awsKeyId,
awsSecretAccessKey,
awsRegion,
} = env.getAWS({ region: 'region' });
Fetch URL objects from url strings
env.getUrl('ENDPOINT')
env.getUrl('FAKE_ENDPOINT')
env.getUrl('FAKE_ENDPOINT', 'http://localhost:3000')
About URLs
Why would one use env.getUrl() if one just wishes to grab the url string value? The best reason to use getUrl() and grab the href property is that env.get() doesn't care about the format of the value. Using getUrl() will ensure the url is properly formatted and return a null value if it isn't. In practice, having an invalid url is the same as having no value at all. Then again, it's your code. Do what you want!
As of now, http, redis and postgresql are the only supported protocols. Other protocols will return null. I'm not against adding new protocol support, but these are the ones that seemed most obvious to me. If you want other protocols supported, I'd recommend making a PR. You may create an issue, but I can't guarantee when I'll get around to implementation.
Shortcut Methods
env.num() ==> env.getNumber()
env.bool() ==> env.getBool()
env.list() ==> env.getList()
env.url() ==> env.getUrl()