1.1.0 • Published 5 years ago
dotenv-conf v1.1.0
dotenv-conf
Loads environment variables from .env file to object. Any variables in process.env will overwrite your variables in .env file.
This is dotenv package wrapper with TypeScript support!
Installation
Install with yarn:
$ yarn add dotenv-confInstall with npm:
$ npm install dotenv-confUsage
Create .env file:
ENV=development
HTTP_HOST=127.0.0.1
HTTP_PORT=8080
REDIS_HOST=127.0.0.1
REDIS_PORT=6379Create config.ts file:
import { parse } from 'dotenv-conf'
const data = parse(`${__dirname}/.env`)
export const env = data.ENV
export const isDev = env === 'development'
export const isProd = env === 'production'
export const http = {
host: data.HTTP_HOST || '127.0.0.1',
port: parseInt(data.HTTP_PORT) || 8080,
}
export const redis = {
host: data.REDIS_HOST || '127.0.0.1',
port: parseInt(data.REDIS_PORT) || 6379,
}Use config module in any file of your project:
import { env, isDev, http } from './config'
console.log(env) // string: development
console.log(isDev) // boolean: true
console.log(http.host) // string: 127.0.0.1
console.log(http.port) // number: 8080import * as redis from 'redis'
import { redis as cfg } from './config'
redis.createClient(cfg)