Giter VIP home page Giter VIP logo

custom-env's Introduction

Custom-Env

Custom env is a library built to make development more feasible by allowing multiple .env configurations for different environments. This is done by loading environment variables from a .env.envname file, into the node's process.env object.

Installation

npm install custom-env

Usage

Place this at the top of your application

require('custom-env').env()

Create a .env file in your app's root directory and add the environment variables each on new line:

APP_ENV=dev
DB_HOST=localhost
DB_USER=root
DB_PASS=root

Simple! The process.env is now loaded with the environment variables above.

Example

console.log(process.env.APP_ENV)
console.log(process.env.DB_HOST)
console.log(process.env.DB_USER)
console.log(process.env.DB_PASS)

Expected Output

dev
localhost
root
root

If you want to load from a particular environment, use:

// This loads configuration from staging environment
require('custom-env').env('staging')

Create a .env.staging file in your app's root directory and add the environment variables each on new line:

APP_ENV=staging
DB_HOST=localhost
DB_USER=root
DB_PASS=root

The process.env is now loaded with the environment variables above. This completely overrides process.env.NODE_ENV Try it out:

NODE_ENV=staging node index.js

Example

console.log(process.env.APP_ENV)
console.log(process.env.DB_HOST)
console.log(process.env.DB_USER)
console.log(process.env.DB_PASS)

Expected Output

staging
localhost
root
root

Loading from the current environment

You can load configuration from the current environment with custom-env by passing the first argument of the env() method as true (note: not required in version 2.x) and that's all:

// This Loads the configuration dynamically from to the current enviroment
// Defaults to _dev_ if the environment was set
require('custom-env').env(true)

The env() method

The env() method holds three (3) optional arguments.

  • 1 envname - Specifies the development name, defaults to dev or development,
  • 2 path - Specifies the directory to find configuration files, defaults to current working directory.
  • 3 defaultEnvFallback - Specifies whether to fallback to .env configuration if the specified envname is not found, defaults to true.
require('custom-env').env('dev', 'path/to/custom/path')

dotenv-expand

You can also leverage the dotenv-expand extension to use ENV variable expansion inside your .env files.

For example:

IP=127.0.0.1
PORT=1234
APP_URL=http://${IP}:${PORT}

Using the above example .env file, process.env.APP_URL would be http://127.0.0.1:1234.

What about .env.production?

We strongly recommend that you should not commit and pass .env.production file in production mode, as this file may contain sensitive information.

Change Log

2.0 - 2020-02-21

Fixed

  • Bug #11 Inconsistency in envname loading fixed.
  • Bug #9 Default env config fall back fixed.

Changed

  • You can now load configuration from the current environment by default without passing any argument to env() method

Github Repo

https://github.com/erisanolasheni/custom-env

custom-env's People

Contributors

amcereijo avatar coogie avatar ericrallen avatar erisanolasheni avatar fgriberi avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.