get-env-or-die
Version:
Utility to get and typecast environment variables.
75 lines (60 loc) • 1.91 kB
Markdown
//github.com/hupe1980/get-env-or-die/workflows/build/badge.svg)

> Utility to get and typecast environment variables.
```bash
npm install --save get-env-or-die
```
```typescript
import { getEnv } from 'get-env-or-die';
process.env.HOST = 'hostname';
const host = getEnv('HOST') // => 'hostname'
```
```typescript
import { getEnv } from 'get-env-or-die';
const host = getEnv('HOST', 'localhost') // => 'localhost'
```
```typescript
import { getIntEnv } from 'get-env-or-die';
process.env.PORT = '80';
const port = getIntEnv('PORT', 8080) // => 80
```
```typescript
import { getBoolEnv } from 'get-env-or-die';
process.env.DEBUG = '1';
const isDebug = getBoolEnv('DEBUG', false) // => true
```
```typescript
import { getArrayEnv } from 'get-env-or-die';
process.env.KEYWORDS = 'a,b,c';
const keywords = getArrayEnv('KEYWORDS') // => ['a','b','c']
```
```typescript
import { getUrlEnv } from 'get-env-or-die';
process.env.URL = 'http://example.com';
const url = getUrlEnv('URL') // => new URL('http://example.com')
```
```typescript
import { getDateEnv } from 'get-env-or-die';
process.env.DATE = '2020-11-11';
const date = getDateEnv('DATE') // => new Date('2020-11-11')
```
```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')
```
All functions throw an error if the environment variable is not convertible or the env is missing and no fallback is provided.
[ ](LICENSE)
![Build](https: