Giter Club home page Giter Club logo

node-configenv's Introduction

node-configenv npm version Build Status Coverage Status

Overview

It is just helper for parsing configuration that you passing with environment variables to javascript object.

Use it with node.js version that support es6 specification

Install

$ npm install @dormi/node-configenv --save

Usage

const ConfigEnv = require('@dormi/node-configenv');
const config = new ConfigEnv(configMap, options);

configMap

Format: {[envName]: [envOptions]}

envName

Environment name for setting options

__ will be parsed as deep level

_ will be parsed as camel case

For example:

{
    API__APP_PORT: 8090
}

Will be parsed as:

{
    api: {
        appPort: 8090
    }
}

envOptions

Options object for environment variable. If it not an object it will be used as default value.

required: false - Check for setting environment variable, default: false

default: value - Set default value if environment variable is undefined

options

global: true - Set your config to global.config. default: false

parse: true - Parse strings to primitives. e.g "[1, 2, 3]" will be parsed to array of numbers [1, 2, 3]. default: false

envFile: true - Load environment variables from file. If true it will search .env file in project root. Can be string path. default: false

Examples

Use with command line environment

Create configuration:

const ConfigEnv = require('@dormi/node-configenv');
const config = new ConfigEnv({
    PORT: {default: 8080},
    MONGO__URL: {default: 'mongodb://localhost:27017/dbname'}
}, {global: true, parse: true});

Run node.js program with env variables:

PORT=9000 node index.js

global.config will be:

{
    port: 9000,
    mongo: {
        url: 'mongodb://localhost:27017/dbname'
    }
}

Use with environment file

Create configuration:

const ConfigEnv = require('@dormi/node-configenv');
const config = new ConfigEnv({
    PORT: {default: 8080},
    MONGO__URL: {default: 'mongodb://localhost:27017/dbname'}
}, {global: true, parse: true, envFile: true});

Create .env file in project root:

PORT=9000
MONGO__URL=mongodb://localhost:27020/some

global.config will be:

{
    port: 9000,
    mongo: {
        url: 'mongodb://localhost:27020/some'
    }
}

node-configenv's People

Contributors

andrewdormi avatar

Stargazers

Dmitry avatar

Watchers

James Cloos avatar  avatar Dmitry 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.