Socket
Socket
Sign inDemoInstall

@smpx/cfg

Package Overview
Dependencies
2
Maintainers
2
Versions
12
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @smpx/cfg

Configuration management for node.js


Version published
Weekly downloads
5
decreased by-50%
Maintainers
2
Created
Weekly downloads
 

Readme

Source

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

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).

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 a overwview of all the functions provided.

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

Keywords

FAQs

Last updated on 12 Dec 2019

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc