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

mh-config

v1.0.2

Published

mh-config is a zero-dependency module that helps provide configuration to your existing or new applications in no time

Downloads

6

Readme

mh-config

mh-config is a zero-dependency module that helps provide configuration to your existing or new applications in no time.

Storing configuration separate from code (not hard-coding) is based on The Twelve-Factor App methodology.

TOC

Introduction

Key features

  • Specify configuration in any file format: ini/json/js
  • Provide these configurations either via a config object or simply process environment variables
  • No need to edit your application's code, if you'd like it that way
  • Hot reload the config file without restarting or reloading your application

Comparison

How mh-config is different (& hopefully better) from the dotenv module?

  • mh-config accepts a number of files formats such as: ini/json/js
    • So you don't need to change the format of your existing config file
    • Using a '.js' file provide eve more flexibility; you can dynamically create configuration values using javascript e.g. modifying time as per day light saving
  • mh-config can be configured to hot reload the config file while your application is still running. This mean no downtime for your app!
    • This also provides the ability to modify the process environment variables available to your application on the fly

Installation

# with npm
npm install mh-config

# or with Yarn
yarn add mh-config

Usage

Two usages are possible:

  1. Usage-1: Include it in your application's code
  2. Usage-2: Use without modifying your application's code

Usage-1: Include it in your module/code

const config = require('mh-config')({debug:true}); 
// pass in options like debug = true

if (config.error) {
  throw config.error
}

// assuming a file named config.ini exists with username=notadmin, then

// USE either of the following:
process.env['username']
// OR
config['username']

Usage-2 Without modifying your code

node -r mh-config/do <yourApp.js> config_option_debug=true
      # additional config options ^^^^^^^^^^^^^^^^^^^^^^^^
// yourApp.js

// USE either of the following:
process.env['username']
// OR
config['username']

The config file

By default, mh-config looks for a suitable file in the current working directory (cwd), usually the root of the project. A suitable file is one of the following formats:

  • .ini
  • .json
  • .js

Examples:

; config.ini
username=trump
password="2020"
port=1880
// config.json
{
    "username": "trump",
    "password": "2020",
    "port": 1880
}
// config.js
module.exports = {
    "username": "trump",
    "password": "2020",
    "port": 1880
}

Options

mg-config takes in config in two ways:

  1. For Usage-1: via an object
  2. For Usage-2: via command line arguments

| name (for usage-1) | name (for usage-2) | description | accepted values | default | | -------------------- | ---------------------------------- | ---------------------------------------------------------------------------- | --------------- | ------- | | filename | config_option_filename | the filename (/with path) where config exists. Should be relative to the CWD | | '' | | setEnv | config_option_setEnv | whether to set process's environment variable as well | true/false | true | | overwriteExistingEnv | config_option_overwriteExistingEnv | whether to overwrite an existing environment variable | true/false | false | | watchIntervalMs | config_option_watchIntervalMs | hot reload any changes to the config file every n milliSeconds | | 5000 | | debug | config_option_debug | whether to print debug messages on console | true/false | false |

Rules

  • Each config values is converted into number if possible, unless surrounded by double/single quotes
  • If filename is not provided, mh-config looks for these file names in CWD and stops looking if it finds one:
    • config.ini
    • config.json
    • config.js
  • mh-config will never mess with your existing process.env unless you specifically ask it to by setting 'overwriteExistingEnv = true'
  • Double and Single quotes are useful to mark a value as text e.g. "100" will be treated as a string while 100 as a number. This is only applicable for .ini files and command line arguments with -r option

License

It's an MIT license; feel free to use as you will.