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

dynamic-config

v1.0.0

Published

Loads configuration files depending on the given env

Downloads

39

Readme

dynamic-config

Dynamic configuration files

Dependency Status Build Status Coverage Status

Loads configuration files depending on:

  • argv: node app.js --env production
  • env: export NODE_ENV=production; node app.js

Expects a .js file as config so you can add dynamic content.

Installation

npm install dynamic-config --save

Options

defaultEnv: string = "develop"

Define which env should be set as default.

log: boolean = false

Enable logging of path/env resolution.

envName: string = "env"

The argument/env variable name we expect.

Example

// config/index.js

const DynamicConfig = require("dynamic-config");
const dynamicConfig = new DynamicConfig({
    defaultEnv: "develop",
    log: true
});

module.exports = dynamicConfig.load(__dirname, "config.js");
// config/develop/config.js

module.exports = {
    whereami: "develop"
}
// app.js
const config = require("./config");

console.log(config);
node app.js

{ whereami: 'develop' }

// Set environment via args
node app.js --env prod

{ whereami: 'prod' }

// Set environment via env
export env=stage; node app.js

{ whereami: 'stage' }

Plugins

extend

These plugins allow you to override specific config fields by applying them via env, argv or a separate local config file.

const dynamicConfig = new (require("dynamic-config"))();

// extend from env
dynamicConfig.use(require("dynamic-config/plugins/extend/env"));

// extend from file
dynamicConfig.use(require("dynamic-config/plugins/extend/file"));

// extend from argv
dynamicConfig.use(require("dynamic-config/plugins/extend/argv"));

module.exports = dynamicConfig.load(__dirname, "config.js");

Hint: The order in which the plugins are applied is important. In the above code snippet, config fields defined via arguments would override their counterparts from an override file, which itself overrides fields from environment variables. This is probably a suitable order for most projects.

node app.js

{ name: 'superApp', port: 9000 }

// Overwrite via argv
node app.js --port 80
// ... or ...
node app.js --port=80

{ name: 'superApp', port: 80 }

// Overwrite via env
export port=90 node app.js

{ name: 'superApp', port: 90 }

// Order matters...
export port=90; node app.js --port 80

{ name: 'superApp', port: 80 }

Extend via file

Create a file named the same as your config, but contains .local in front of the extension, like config.js becomes config.local.js.

In the config extension file you can define any subset of the object, that is defined in the main config and it would overwrite the corresponding value. Both configs are merged via deep-assign.

// config.js
module.exports = {
    a: 1,
    b: {
        c: "c",
        d: 2
    },
    e: 3
}

// config.local.js
module.exports = {
    e: "e",
    b: {
        d: "d"
    }
}

// result
{
    a: 1,
    b: {
        c: "c",
        d: "d"
    },
    e: "e"
}