dotenv-conf

Loads environment variables from .env file to object

Usage no npm install needed!

<script type="module">
  import dotenvConf from 'https://cdn.skypack.dev/dotenv-conf';
</script>

README

dotenv-conf

npm version npm weekly downloads install size dotenv version

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-conf

Install with npm:

$ npm install dotenv-conf

Usage

Create .env file:

ENV=development

HTTP_HOST=127.0.0.1
HTTP_PORT=8080

REDIS_HOST=127.0.0.1
REDIS_PORT=6379

Create 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: 8080
import * as redis from 'redis'
import { redis as cfg } from './config'

redis.createClient(cfg)