1.4.1 • Published 6 years ago

environmentconfig v1.4.1

Weekly downloads
7
License
MIT
Repository
github
Last release
6 years ago

environmentconfig

Introduction

A node.js package for most simple config data loading based on app environment from CSON or JSON file(s).

This method for loading configs I use now for few years and I think it's time to create some united package.

So there it is.

Goals

To load config object from files or folder almost withnout configuration. Of course with environments inheritance.

Instalation

npm install environmentconfig

Examples

Single file config

At first we need to create CSON config file

production:
    host: #{process.env.HOST}
    port: #{process.env.PORT}

development:
    port: 8080
    
localhost:
    host: '127.0.0.1'
    port: #{process.env.PORT || 3000}

Then you can create .env file with ENV variables

PORT=80
HOST="10.0.0.1"

Load without config definition

In this case default configuration is the last one. So localhost.

var configLoader = require('environmentconfig');
var config = configLoader();

result:

{
    host: '127.0.0.1'
    port: 8080
}

Change environment with ENV variable

Default ENV variable name is NODE_ENV

// index.js
var configLoader = require('environmentconfig');
var config = configLoader();

run command:

NODE_ENV=development node index.js

result:

{
    host: '127.0.0.1'
    port: 8080
}

Change ENV variable name

You can change ENV variable name in config via parameter envName

// index.js
var configLoader = require('environmentconfig');
var config = configLoader({
    envName: 'ENVIRONMENT'
});

run command:

ENVIRONMENT=production node index.js

result:

{
    host: '10.0.0.1'
    port: 80
}

Multiple environments config file

At first we need to create few files

// config/config.production.json
{
    "host": "10.0.0.1",
    "port": "80"
}
// config/config.development.json
{
    "port": "8080"
}
// config/config.localhost.json
{
    "host": "127.0.0.1"
}
// index.js
var configLoader = require('environmentconfig');
var config = configLoader({
    type: 'json',
    dir: './config',
    isFolderStructure: true
});

run command:

node index.js

result:

{
    host: '127.0.0.1'
    port: 8080
}

Own environments

Default environments is production, test, development, localhost. But you can define your own:

var configLoader = require('environmentconfig');
var config = configLoader({
    environments: [
            'prod',
            'stage',
            'dev'
        ]
});

In this case default environment will be dev because it's last one.

Dependencies works like waterfall. If you select stage, it will inherit everything from prod config and rewrite or append its own values.

Variables injecting

Sometimes you need to pass some data to config and load then directly in it. For example, when you load environment variables some other way then through ENV vars (some json loaded to container in build etc.).

For this king of situations you can use inject option in configLoader config.

For example:

var configLoader = require('environmentconfig');

var injectedVars = {
    server: {
        port: 80
    },
    databases: {
        mysql: {
            host: 'some.mysql.con.string'
        }
    }
};

var config = configLoader({
    inject: injectedVars
});

After that you can load this variables in config via #{...} syntax like that:

production:
    port: #{server.port}
    mysql: #{databases.mysql.host}

development:
    port: 8080

localhost:
    port: 3000

Config API

There is a list of configuration options for config loading

PropertyDescriptionValueDefault
typeType of config filescson or jsoncson
envNameWitch ENV variable define environments name. It works only if property environment is not setstringNODE_ENV
environmentFor defining current environment name your own waystringLast value of environments property
environmentsArray of all app environments with waterfall inheritancestring array[ 'production', 'test', 'development', 'localhost' ]
isFolderStructureDefine if configs are in multiple filesboolfalse
dirPath to folder with configsstringStarting point of app (process.cwd())
fileDefine specific file what you want load, ignoring other optionsstringnull
filePrefixIf your config file name is not starting with config (config.cson or config.production.cson). You cant change it here.stringconfig
envFilePathIf you have .env file on different path with different namestringnull
injectYou can inject variables for loading them via #{...} in configobject`{}
1.4.1

6 years ago

1.3.1

6 years ago

1.2.4

6 years ago

1.2.3

6 years ago

1.2.2

6 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago