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

config-provider

v2.0.1

Published

Configurations Providers for Node.js

Downloads

45

Readme

Config provider

A service module to load global app configurations from files and join environment variables together Filter those by the environment (staging, production, test, development, etc)
And overwrite some of those with ENV variables

Install & import

npm install config-provider
const ConfigProvider = require( 'config-provider' );

Load config files

Eager loading

All .json and .js files inside a folder called /config in app root will be read as soon as this module is required by the project, so no actions required.

Lazy loading

But, you can setup a alternative folder containing your configuration files with the .load() method:

ConfigProvider.load( './path/to/my/configs' );

The parameter path is a string, and should be relative to the current folder.

Accessing configurations

Just use the (.get() method, with the path to the value, like this:

const value = ConfigProvider.get( 'general.system.input.type' );

In this example, there was a file called general.json with this content:

{
  "system": {
    "input": {
      "type": "dynamic"
    }
  }
}

So the path will be like acessing properties of a JS object, where the first is the file name.

Note that the file extension is omitted when accessing the value

Applying ENV variables to the configs

All environment variables which names are compliant with the IEEE Std 1003.1-2001 norm (upper case letters, _ and numbers) are automatically applied to the configs, and can be accessed the same way as any other config.

process.env.FOO = 'bar';
const ConfigProvider = require( 'config_provider' );

ConfigProvider.get( 'foo' ) === 'bar'; // true

Overwriting via ENV variables

You can overwrite any configuration, even nested ones, via the environment variables, just use __ (double underscores) to indicate a nexted value:

Given a configs.json:

{
  "colors": {
    "red": "#00ff00"
  }
}

And a main file:

process.env.CONFIGS__COLORS__RED = 'red';
const ConfigProvider = require( 'configs.json' );

ConfigProvider.get( 'configs.colors.red' ) === 'red'; // true

NODE_ENV sensitive configurations

If in the same config file you have configurations for more than one environment, like staging, production and development, the ConfigProvider will read just the appropriate one according to the current process.env.NODE_ENV:

Given a config.json:

{
  "production": {
    "token": "23983748367394"
  },
  "staging": {
    "token": "fake_token"
  }
}

And a main file:

process.env.NODE_ENV = 'staging';
const ConfigProvider = require( 'configs.json' );

ConfigProvider.get( 'token' ) === 'fake_token'; // true

Note that you do not need to write the NODE_ENV name in the path to access the value using the .get()

Supported files

Both .json and .js files are read as configurations. For .js files, it must export a literal object to work:

A config.js file:

module.exports = {
  config: {
    color: 'red'
  }
};

Environment variables values parsing

Any ENV variable which the value is a number or a boolean, will be converted when read:

process.env.SOME_VALUE = '3';
const ConfigProvider = require( 'configs.json' );

ConfigProvider.get( 'some_value' ) === 3; // true

Notes

  • Don't use camelCase on the configurations, prefer sneak_case.

  • This was tested with pm2, mocha, node shell and node script.