https://github.com/joshuaavalon/read-env-vars
Read environment variables to JSON
https://github.com/joshuaavalon/read-env-vars
env environment-variables json
Last synced: 5 months ago
JSON representation
Read environment variables to JSON
- Host: GitHub
- URL: https://github.com/joshuaavalon/read-env-vars
- Owner: joshuaavalon
- License: apache-2.0
- Created: 2022-11-10T07:30:20.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2023-09-11T04:00:29.000Z (almost 3 years ago)
- Last Synced: 2025-03-27T13:42:34.601Z (over 1 year ago)
- Topics: env, environment-variables, json
- Language: TypeScript
- Homepage:
- Size: 196 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# read-env-vars
[](https://www.npmjs.com/package/read-env-vars)
[](https://www.npmjs.com/package/read-env-vars)
[](./LICENSE)
## Getting Started
```sh
npm i read-env-vars
```
## Usage
```typescript
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
```ini
# 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.
```ini
# Mapped as `{ "key1": { "key2": "a" } }`
PREFIX__KEY1__KEY2=a
```
### Array
By ending a key with `_`, it will be considered as an array.
```ini
# Mapped as `{ "key1": ["a"] }`
PREFIX__KEY1_0=a
```
## Options
#### `source`
- Type: `Record`
- Default: `process.env`
Source of the environment variables
#### `separator`
- Type: `string`
- Default: `__`
Separator of object key
#### `camelCase`
- Type: `camelcase.Options`
- Default: `undefined`
[Options][camelcase-api] passed to [camelcase].
[camelcase]: https://github.com/sindresorhus/camelcase
[camelcase-api]: https://github.com/sindresorhus/camelcase#api
#### `onEmpty`
Value set when handle empty value
- Type: `"null" | "undefined" | "empty" | "ignored"`
- Default: `ignored`