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

multiconfig

v1.1.6

Published

A recursive, hierarchical, multi-file, environment based configuration loader

Downloads

47

Readme

node-multiconfig

NPM

Build Status Coverage Status Dependency Status devDependency Status

A recursive, hierarchical, multi-file, environment based configuration loader. It supports JSON, YAML, INI and CSON.

This module loads multiple .json files recursivly. It support different configurations for different environments (using the NODE_ENV environment variable). Inside configuration files you can specify environment variable placeholders using a prefix. All configurations for the given environment will be loaded into an object.

Installation

Install the module:

npm install multiconfig --save

Usage

In order to use this module, configuration files must be structured in a certain way. A config directory must be present in the root directory of your project. The config directory should have a default directory, in which default configurations are stored.

mkdir -p config/default

In your application configurations are accessible as an object, using the same pattern as the directory structure. By default, directory and file names are converted to camel case (this can be disabled).

Each directory can contain a file named index (which can be any of the support extensions, for example index.yaml). All the values of this file be a property of the directory name. (see example)

Optionally, for each environment (e.g. development, staging or production) another directory is created. In this directory you can create configurations that will override default configurations. The supportedd file types can be mixed. The environment is specified with the NODE_ENV environment variable.

mkdir config/production

Usage

var multiconfig = require('multiconfig');

var config = multiconfig.load();

// Do whatever you want with the 'config' object, 
// 'config' is just a regular Javascript object.

Optionally you can pass an options object as the first argument.

var options = {
    directory: '/path/to/config/directory', //specify the config directory (can be relative or absolute)
    env: 'staging', // Specify the environment
    prefix: '$ENV_VAR',
    pathCase: false // Don't convert directorie/file names to camelCase.
    configVariation: 1 // Use configs suffixed with "-1", "-2", etc. 
};

var config = multiconfig.load(options);

The default prefix is $ENV. It uses : as a separator. An example value in a configuration file: $ENV:YOUR_ENVIRONMENT_VAR_HERE. The value will be replaced by the actual value of the environment variable, if it exists. Check the example below.

The config variation to use can also be set using NODE_CONFIG_VARIATION environemnt variable. The config directory can also be specified using NODE_CONFIG_DIRECTORY an environment variable. Environment variables will have priority over inline configuration.

Example

This example uses JSON and YAML. other formats will work in the same way, just a different syntax. Assume we have an environment variable TEST_DATA with the value awesome. We have the following configuration files (note the paths):

./config/default/index.yaml

applicationName: multiconfig

./config/default/database/mongo.json

{
  "host": "127.0.0.1",
}

./config/default/compilers/java.json

{
  "version": "7",
  "name": "Java"
}

./config/production/compilers/java.json

{
  "version": "6",
  "debug": false  
}

./config/production/database/redis-config.json

{
  "database": "$ENV:TEST_DATA",
  "host": "10.0.0.1",
  "port": 9920
}

If an application is run without any environment specified, this will be the output of the configuration loader:

{
  "applicationName": "multiconfig",
  "compilers": {
    "java": {
      "version": "7",
      "name": "Java"
    }
  },
  "database": {
    "mongo": {
      "host": "127.0.0.1"
    }
  }
}

Note the camel case conversion of the redis-config file. By default every directory or filename will be converted to camel case.

If we run the application with NODE_ENV=production the output will be the following:

{
  "applicationName": "multiconfig",
  "compilers": {
    "java": {
      "version": "6",
      "name": "Java",
      "debug": false
    }
  },
  "database": {
    "mongo": {
      "host": "127.0.0.1"
    },
    "redisConfig": {
      "database": "awesome",
      "host": "10.0.0.1",
      "port": 9920
    }
  }
}