envparse
v2.0.0
Published
Parse environment variables to a deeply nested object
Maintainers
Readme
This is a tiny library for NodeJS which parses environment variables and converts them into a deeply nested object.
index.ts
import envparse from "envparse";
console.log(envparse('MYAPP'))$ MYAPP__USER=bobby node index.js
{ user: "bobby" }$ export MYAPP__DB__USERNAME=root
$ export MYAPP__DB__PASSWORD=supersecretpassword
$ node index.js
{ database: { username: "root", password: "supersecretpassword" } }API
parseEnv(opts)
import { parseEnv } from "envparse";Where opts is an object containing:
- prefix: the prefix that will be searched for (optional)
- env: an object containing the environment variables (optional)
If opts is a string, it will use that string as a prefix.
Examples:
parseEnv('MYAPP');const config = parseEnv({
prefix: 'MYAPP',
env: {
MYAPP__FOO: 'teststring',
MYAPP__BAR: 'anotherteststring',
}
});parseValue(text)
import { parseValue } from "envparse";Parse a single environment variable's value into a JavaScript value.
- The literals
trueandfalseconvert to boolean values - If the value is convertible to
Number, it will do so - In all other cases it will return the value as plaintext
In a future version JSON objects and arrays will also be accepted.
License
The MIT License
