1 | # dotenv-conf
|
2 |
|
3 | [![npm version](https://img.shields.io/npm/v/dotenv-conf)](https://npmjs.com/package/dotenv-conf)
|
4 | [![npm weekly downloads](https://img.shields.io/npm/dw/dotenv-conf)](https://npmjs.com/package/dotenv-conf)
|
5 | [![install size](https://packagephobia.com/badge?p=dotenv-conf)](https://packagephobia.com/result?p=dotenv-conf)
|
6 | [![dotenv version](https://img.shields.io/github/package-json/dependency-version/vovarevenko/dotenv-conf/dotenv)](https://npmjs.com/package/dotenv)
|
7 |
|
8 | Loads environment variables from `.env` file to object. Any variables in `process.env` will overwrite your variables in `.env` file.
|
9 |
|
10 | This is [dotenv](https://npmjs.com/package/dotenv) package wrapper with TypeScript support!
|
11 |
|
12 | ## Installation
|
13 |
|
14 | Install with [yarn](https://yarnpkg.com):
|
15 |
|
16 | ```bash
|
17 | $ yarn add dotenv-conf
|
18 | ```
|
19 |
|
20 | Install with [npm](https://npmjs.com):
|
21 |
|
22 | ```bash
|
23 | $ npm install dotenv-conf
|
24 | ```
|
25 |
|
26 | ## Usage
|
27 |
|
28 | Create `.env` file:
|
29 |
|
30 | ```dosini
|
31 | ENV=development
|
32 |
|
33 | HTTP_HOST=127.0.0.1
|
34 | HTTP_PORT=8080
|
35 |
|
36 | REDIS_HOST=127.0.0.1
|
37 | REDIS_PORT=6379
|
38 | ```
|
39 |
|
40 | Create `config.ts` file:
|
41 |
|
42 | ```typescript
|
43 | import { parse } from 'dotenv-conf'
|
44 |
|
45 | const data = parse(`${__dirname}/.env`)
|
46 |
|
47 | export const env = data.ENV
|
48 | export const isDev = env === 'development'
|
49 | export const isProd = env === 'production'
|
50 |
|
51 | export const http = {
|
52 | host: data.HTTP_HOST || '127.0.0.1',
|
53 | port: parseInt(data.HTTP_PORT) || 8080,
|
54 | }
|
55 |
|
56 | export const redis = {
|
57 | host: data.REDIS_HOST || '127.0.0.1',
|
58 | port: parseInt(data.REDIS_PORT) || 6379,
|
59 | }
|
60 | ```
|
61 |
|
62 | Use `config` module in any file of your project:
|
63 |
|
64 | ```typescript
|
65 | import { env, isDev, http } from './config'
|
66 |
|
67 | console.log(env) // string: development
|
68 | console.log(isDev) // boolean: true
|
69 |
|
70 | console.log(http.host) // string: 127.0.0.1
|
71 | console.log(http.port) // number: 8080
|
72 | ```
|
73 |
|
74 | ```typescript
|
75 | import * as redis from 'redis'
|
76 | import { redis as cfg } from './config'
|
77 |
|
78 | redis.createClient(cfg)
|
79 | ```
|