npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

enco

v2.1.1

Published

Load config from YAML, JSON or CSON file/folder base on environment name.

Downloads

46

Readme

enco

Introduction

A node.js package for most simple config data loading based on app environment from YAML, 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 enco

Examples

Single file config

At first we need to create YAML 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('enco');
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('enco');
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('enco');
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('enco');
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('enco');
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('enco');

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

| Property | Description | Value | Default | |----------|-------------|-------|---------| | type | Type of config files | cson or json | yaml | | envName | Witch ENV variable define environments name. It works only if property environment is not set | string | NODE_ENV | | environment | For defining current environment name your own way | string | Last value of environments property | | environments | Array of all app environments with waterfall inheritance | string array | [ 'production', 'test', 'development', 'localhost' ] | | isFolderStructure | Define if configs are in multiple files | bool | false | | dir | Path to folder with configs | string | Starting point of app (process.cwd()) | | file | Define specific file what you want load, ignoring other options | string | null | | filePrefix | If your config file name is not starting with config (config.cson or config.production.cson). You cant change it here. | string | config | | envFilePath | If you have .env file on different path with different name | string | null | | inject | You can inject variables for loading them via #{...} in config | object | `{} |