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

@smpx/cfg

v1.4.1

Published

Configuration management for node.js

Downloads

40

Readme

cfg

Configuration management for node.js.

Example config:

// config.js
module.exports = {
    db: {
        password: 'abcde',
        host: '127.0.0.1',
    },
    port: 3000,
    hosts: ['127.0.0.1'],
    logsDir: `${__dirname}/logs`,

    $env_production: {
        port: 80,
        logsDir: '/home/app/logs',
    },

    $env_test: {
        port: 5000,
    },

    $env_CI: {
        db: {
            // Docker image hostname
            host: 'postgresql',
        },
    },
};

Usage:

const cfg = require('@smpx/cfg');

const dbConf = cfg('db'); // { password: 'abcde', host: '127.0.0.1' }
const dbPassword = cfg('db.password');

Docs

It reads values from config.js file from project directory, but they can be overwritten with another config.js in the private folder in the project directory. Or through env vars in this format:

# Overwriting password (db.password):
CFG__DB__PASSWORD='password' yarn start
# Adding host to posiition 1 (hosts.1):
CFG__HOSTS__1='new-host.region.rds.amazonaws.com' yarn start
# Override all hosts
CFG__HOSTS='@JSON:["a.b", "c.d"]' yarn start

NOTE: ENV VARS override might only work with camelCase keys

It basically uses lodash.set internally. The path is generated by removing the CFG__ prefix and replacing __ with . and converting each word in between to camelCase (also through lodash). If the value starts with @JSON:, it will be parsed as JSON (after removing @JSON:), so you can use it to set arrays, objects and numbers.

NODE_ENV & CI overrides

cfg also allows overriding config according to NODE_ENV or CI environment variables. For example if NODE_ENV="production", then if a $env_production key exists it's value gets merged over existing conf (this happens before merging any private/config.js file).

Similarly in CI environments, the value in $env_CI is merged.

API

Please check out the typescript definition file: index.d.ts for an overview of all the functions provided.

Typescript

For getting types of the output types, you can define a typedef in your config.js file like:

//config.js

const config = {
    db: {
        password: 'abcde',
        host: '127.0.0.1',
    },
};

/** @typedef {typeof config} ConfigType */

module.exports = config;

And in a global typings file in your project, like global.d.ts, import it and set this as the BaseConfig:

// global.d.ts
import { ConfigType } from './config';

declare global {
  interface BaseConfig extends ConfigType {}
}

This will be automatically picked by cfg. You can also modify this type with some custom keys available only through env vars:

// global.d.ts
import { ConfigType } from './config';

declare global {
    interface BaseConfig extends ConfigType {
       envVarOnlyKey?: string;
    }
}

CLI

Get a value from cfg.js

# Installed globally
cfg get redis.port
cfg get logsDir

# See how ENV_VAR will override config
CFG__DB__PASSWORD='password' cfg get "db.password"

# Through npx or yarn (when installed locally)
npx cfg get redis.port
yarn cfg get logsDir