GithubHelp home page GithubHelp logo

rjmk / env2 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dwyl/env2

0.0 2.0 0.0 111 KB

:computer: Simple environment variable (from config file) loader for your node.js app

License: GNU General Public License v2.0

JavaScript 100.00%

env2's Introduction

env2 - environment variable loader

Build Status Test Coverage Code Climate Dependency Status devDependency Status npm

## Why?

The Twelve Factor App section 3 states:

"Store config in the environment"

"An app’s config is everything that is likely to vary between deploys (staging, production, developer environments, etc)".

This means we need a simple/reliable way of managing environment variables; env2 is the way.

What?

env2 allows you to store your environment variables in an env.json file which gets loaded when your app starts.

All the entries in the env.json file are exported as environment variables available as keys in the process.env object.

How?

Create your env.json Configuration File

First create an env.json file in your repo:

{
  "DB_HOST": "127.0.0.1",
  "DB_PORT": 9200,
  "DB_USER": "anon",
  "DB_PASS": "password"
}

If you don't want your env.json to be in your version control (i.e. visible on GitHub) simply add the filename to your .gitignore file:

echo 'env.json' >> .gitignore

Install from NPM

Next install env2 from npm and save it to your package.json file:

npm install env2 --save

Use in your Code

Then in your script/module:

var env = require('env2')('./path-to-your/env.json')

// your app goes here
console.log(process.env.DB_HOST); // "127.0.0.1"

now all the entries in your env.json file are available as keys/values of the process.env Object which means you can use process.env.API_KEY or process.env.DB_PASSWORD in your script. (or what ever you have defined as entries in your env.json)

Env is synchronous; it loads all your configuration variables into the process.env object before app/script execution.


Do you want to Define Priority for Variables?

Do you want the ability to specify the priority which environment variables take precendence? e.g: if you supply a command-line argument when running your script/app:

env=PROD API_KEY=token.dwyl.yolo node myapp.js

We have an open discussion on this: dwyl#1

At present, any environment variable defined in the environment where your app is running (or via command-line arguments) will take precendence over the same key in your env.json file ... if you prefer to have the option to specify the priority, please add a comment to the isssue: dwyl#1


Name ?

Q: Why is it called "env2"?
A: as the digit in the name suggests, there was/is an "env" (version 1): https://www.npmjs.com/package/env written by @dshaw sadly, it was never finished and has not been updated in 4 years ... We asked Dan if he would accept a Pull Request updating the package: dshaw/env#6 and he said he would accept it ... But after investing the time and submitting the pull request: dshaw/env#8 which updated the package to the latest version of Node/io.js and had tests & 100% coverage, the PR got ignored. see: https://twitter.com/dshaw/status/628237150253772801 Not that we're "impatient" but we need to move on with our code/lives. If the PR to env ever gets merged, we could use that instead, but actually env2 is much simpler and servers our needs better, so we are probably going to stick with the "New and Improved" Version.

env2's People

Contributors

dshaw avatar nelsonic avatar

Watchers

 avatar  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.