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

konfig

v0.2.1

Published

Config loader module. Automatic, environment specific and dynamic.

Downloads

689

Readme

KONFIG

Konfig is a config loader module which allows you to load json, cson and yaml files automatically by environment in node.js applications. You can also define dynamic values which can be used especially for dynamic environment variables on Heroku like services.

Installation

First intall module from npm :

$ npm install konfig

Create a folder named config under the root directory of the project. Then load Konfig in your application file :

var config = require('konfig')()

// Don't forget parentheses to call the function

We recommend you to define config variable as global :

global.config = require('konfig')()

You can also use different folder name except config by passing path variable while loading Konfig :

global.config = require('konfig')({ path: './another_directory' })

Usage

Let's create an example config file under config folder named app.json or app.yml

{
    "default": {
        "port": 3000,
        "cache_assets": true,
        "secret_key": "7EHDWHD9W9UW9FBFB949394BWYFG8WE78F"
    },

    "development": {
        "cache_assets": false
    },

    "test": {
        "port": 3001
    },

    "staging": {
        "port": #{process.env.PORT},
        "secret_key": "3F8RRJR30UHERGUH8UERHGIUERHG3987GH8"
    },

    "production": {
        "port": #{process.env.PORT},
        "secret_key": "3F8RRJR30UHERGUH8UERHGIUERHG3987GH8"
    }
}

If you use yaml format the config file will be like this :

default:
    port: 3000
    cache_assets: true
    secret_key: 7EHDWHD9W9UW9FBFB949394BWYFG8WE78F

development:
    cache_assets: false

test:
    port: 3001

production: &production
    port: #{process.env.PORT}
    secret_key: 3F8RRJR30UHERGUH8UERHGIUERHG3987GH8

# aliases must be defined before you include!
staging:
    <<: *production

After creating config files, let's try it by using node interpreter. But first ensure that the module is installed npm install konfig and open node interpreter in the root directory of the project :

$ node

Then load the module

> var config = require('konfig')()

Let's try to get port and secret key values, remember that the default environment will be development and the config object structure will be config.[filename].[config_key]...

> config.app.port
3000
> config.app.secret_key
'7EHDWHD9W9UW9FBFB949394BWYFG8WE78F'

Quit interpreter, and let's open the interpreter with staging environment with port 4567. If you look at the config file you will see the staging port is dynamic value. Konfig enables you to use node variables in your config file by using #{} signs.

$ NODE_ENV=staging PORT=4567 node
> var config = require('konfig')()

Now, we will get some config values:

> config.app.port
4567
> config.app.secret_key
'3F8RRJR30UHERGUH8UERHGIUERHG3987GH8'

Notice that default values copies itself to the config if there is no key in the environment config with the same name.

License

Konfig is released under GNU Lesser General Public License v3 (or higher) published by Free Software Foundation. See http://www.gnu.org/licenses/lgpl-3.0.html for more details.