https://github.com/hupe1980/get-env-or-die
Utility to get and typecast environment variables.
https://github.com/hupe1980/get-env-or-die
env environment-variables
Last synced: 6 months ago
JSON representation
Utility to get and typecast environment variables.
- Host: GitHub
- URL: https://github.com/hupe1980/get-env-or-die
- Owner: hupe1980
- License: mit
- Created: 2021-09-09T15:38:47.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2022-03-07T00:44:20.000Z (over 3 years ago)
- Last Synced: 2025-03-12T01:46:46.551Z (7 months ago)
- Topics: env, environment-variables
- Language: TypeScript
- Homepage:
- Size: 2.42 MB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# get-env-or-die

> Utility to get and typecast environment variables.
## Installation
```bash
npm install --save get-env-or-die
```## How to use
### String environment variable
```typescript
import { getEnv } from 'get-env-or-die';
process.env.HOST = 'hostname';
const host = getEnv('HOST') // => 'hostname'
```### Optional Fallback
```typescript
import { getEnv } from 'get-env-or-die';
const host = getEnv('HOST', 'localhost') // => 'localhost'
```### Int environment variable
```typescript
import { getIntEnv } from 'get-env-or-die';
process.env.PORT = '80';
const port = getIntEnv('PORT', 8080) // => 80
```### Boolean environment variable
```typescript
import { getBoolEnv } from 'get-env-or-die';
process.env.DEBUG = '1';
const isDebug = getBoolEnv('DEBUG', false) // => true
```### String array environment variable
```typescript
import { getArrayEnv } from 'get-env-or-die';
process.env.KEYWORDS = 'a,b,c';
const keywords = getArrayEnv('KEYWORDS') // => ['a','b','c']
```### Url environment variable
```typescript
import { getUrlEnv } from 'get-env-or-die';
process.env.URL = 'http://example.com';
const url = getUrlEnv('URL') // => new URL('http://example.com')
```### Date environment variable
```typescript
import { getDateEnv } from 'get-env-or-die';
process.env.DATE = '2020-11-11';
const date = getDateEnv('DATE') // => new Date('2020-11-11')
```### RegExp environment variable
```typescript
import { getRegExpEnv } from 'get-env-or-die';
process.env.REG_EXP = '/ab+c/i';
const regExp = getRegExpEnv('REG_EXP') // => new RegExp('ab+c', 'i')
```### Errors
All functions throw an error if the environment variable is not convertible or the env is missing and no fallback is provided.## License
[MIT](LICENSE)