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

wild-config

v1.7.1

Published

Configuration management module

Downloads

3,122

Readme

wild-config

Opinionated configuration management module for Node.js daemon applications.

  • Config files use either toml, JSON or JavaScript syntax
  • If the config file format is JavaScript, then the value must be exported with "module.exports = {...}"
  • The application can have a config file for default values in ./config/default.toml
  • Main config file path can be provided from a command-line argument, e.g. --config=/etc/app.toml
  • Additionally, command-line arguments can be used to override any existing config option

Loading order

wild-config tries to load configuration in the following order (missing files are skipped, except the one provided by --config argument). Values are merged.

  1. ./config/default.*
  2. ./config/$NODE_ENV.*
  3. NODE_CONFIG_PATH environment value or --config argument value
  4. APPCONF_* prefixed environment variables
  5. command line arguments

If you want to use a different configuration directory than './config' for default configuration files, then set it with the NODE_CONFIG_DIR environment variable

Environment variables

When using environment variables to provide config values, only such keys are merged that already exist in the configuration object, so you have to define a default value in the config file. Use underscores instead of dots for subkeys. Note that all underscores are interpreted as dots when parsing, which means keys with underscores can't be overriden with environment variables.

Example config/default.toml:

[server]
apiPort=3000

Override server.apiPort value with the following environment variable:

APPCONF_server_apiPort=80

This resolves into the following config structure:

{
  server: {
    apiPort: 80
  }
}

Command line arguments

Like with the environment variables, when using command-line arguments to provide config values, only such keys are merged that already exist in the configuration object. For subkeys, use dot notation. Value type (numbers, booleans, and strings are supported) is defined by existing value.

Example config/default.toml:

[server]
enabled=false

Override server.enabled value with the following command-line argument:

node app.js --server.enabled=true

server.enabled is defined as a boolean in the config file, so the overridden value is also going to be true as a boolean and not "true" as a string.

TOML extensions

wild-config toml includes additional options when working with toml

Including child files

Use the following syntax to include an additional config file in the place of the directive.

# @include "/path/to/sub/config.toml"

This directive also works in a nested object.

[nested]
    # @include "/path/to/sub/config.toml"

You can also use wildcards to load data from multiple files.

# @include "/path/to/sub/*.toml"
# @include "/path/to/sub/**/*.toml"

Notes

  • Included paths are resolved relative to the path of the configuration file where the include directive is used
  • Included config files do not have to be toml files. Any other supported format works as well
  • If the included config file is a toml file, then it can have its own includes
  • If the config file returns an array, then the array value will become the value of the parent key of the directive only if there are no other subkeys at the same level as the directive
  • In case of duplicate keys, included file will always override the values regardless if @include is placed before or after the declaration
  • Special value {ENV} is replaced in all file paths by the NODE_ENV value

Application config file

If you are running your app as a service daemon, you can load configuration from a config file using the --config argument. These values are loaded and merged with the default values.

[Service]
WorkingDirectory=/opt/app
ExecStart=/usr/bin/node index.js --config=/etc/app.toml

Usage

const config = require('wild-config');
console.log(config.server.enabled);

Limitations

  • You can not use "on" as a root key. If you do then it is ignored. This key is reserved for the event emitter handler.
  • When providing configuration options from command line then --config does not override root.config value (if it even exists). This argument is used only for defining the configuration file path.

License

MIT