https://github.com/webpod/envapi
An API to interact with environment files
https://github.com/webpod/envapi
dotenv env
Last synced: about 1 year ago
JSON representation
An API to interact with environment files
- Host: GitHub
- URL: https://github.com/webpod/envapi
- Owner: webpod
- License: mit
- Created: 2024-12-29T11:06:43.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-04-06T11:38:23.000Z (about 1 year ago)
- Last Synced: 2025-04-06T12:28:22.233Z (about 1 year ago)
- Topics: dotenv, env
- Language: TypeScript
- Homepage:
- Size: 357 KB
- Stars: 7
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# envapi
> An API to interact with environment files
## Install
```sh
npm install envapi
```
## Usage
```ts
import { parse, stringify, config, load, loadSafe } from 'envapi'
const input = `A=A\nFOO=BAR`
const env = parse(input) // { A: 'A', FOO: 'BAR' }
const output = stringify(env) // A=A\nFOO=BAR
```
### parse()
Parse a dotenv string into an object.
```ts
const raw = `
A=A
FOO=BAR #comment`
const env = parse(raw) // { A: 'A', FOO: 'BAR' }
```
### stringify()
Stringify an object into a dotenv string.
```ts
const env = { A: 'A', FOO: 'BAR' }
const raw = stringify(env) // 'A=A\nFOO=BAR'
```
### load()
Read a dotenv file(s) and parse it into an object. `loadSafe()` suppresses ENOENT errors.
```ts
await fs.writeFile('.env1', 'FOO=BAR')
await fs.writeFile('.env2', 'BAZ=QUX')
const env = load('.env1', '.env2') // { FOO: 'BAR', BAZ: 'QUX' }
const _env = loadSafe('.env.notexists') // {}
```
### config()
Load a dotenv file into `process.env`.
```ts
config('.env1')
process.env.FOO // BAR
```
## License
[MIT](./LICENSE)