read-env-vars
Getting Started
npm i read-env-vars
Usage
import readEnvVars from "read-env-vars";
/**
* PREFIX__KEY1=1
* PREFIX__KEY2=a
* PREFIX__KEY3__KEY=b
* PREFIX__KEY4_0=c
*/
/**
* {
* "key1": 1,
* "key2": "a",
* "key3": { "key": "b" },
* "key4": ["c"]
* }
*/
const vars = readEnvVars("PREFIX");
Every variables MUST start with the prefix and separator (default to __
).
Also, the key will be converted to camel-case (Can be changed).
Scalar
# Mapped as `{ "key1": 1 }`
PREFIX__KEY1=1
# Mapped as `{ "key2": "a" }`
PREFIX__KEY2=a
# Mapped as `{ "key3": true }`
PREFIX__KEY3=true
Object
By using the separator, the key will be considered nested as an object.
# Mapped as `{ "key1": { "key2": "a" } }`
PREFIX__KEY1__KEY2=a
Array
By ending a key with _<number>
, it will be considered as an array.
# Mapped as `{ "key1": ["a"] }`
PREFIX__KEY1_0=a
Options
source
- Type:
Record<string, string | undefined>
- Default:
process.env
Source of the environment variables
separator
- Type:
string
- Default:
__
Separator of object key
camelCase
- Type:
camelcase.Options
- Default:
undefined
onEmpty
Value set when handle empty value
- Type:
"null" | "undefined" | "empty" | "ignored"
- Default:
ignored