Simple Config Utility for Node.js

1.8.2 · abandoned · verified Sun Apr 19

Cz is a minimalistic configuration utility designed for Node.js applications, enabling developers to load, manage, and access configuration settings from files or programmatically. It provides methods for loading configuration files (e.g., JSON), setting default values, and getting/setting specific configuration properties using dot-notation paths. The package's current stable version is 1.8.2, released in early 2017. Due to a lack of updates since then, it is considered abandoned. Key differentiators include its extreme simplicity and reliance on CommonJS patterns, making it suitable for older Node.js projects but not for modern ESM-first environments. It provides basic configuration merging capabilities by applying changes on top of defined defaults, with options to reset the configuration or just its defaults.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate `Cz`, set initial configuration, define default values, retrieve specific configuration properties using dot notation, and construct a practical connection string using both manual concatenation and `cz`'s `joinGets` helper.

const Cz = require('cz');
const config = new Cz();

// Imagine 'config.json' exists with some data, e.g., { "db": { "host": "localhost" } }
// For demonstration, we'll simulate the load with `set`
config.set('db', {
    host: 'localhost',
    port: 27017,
    username: 'user',
    password: 'password',
    collection: 'my-app'
});

// Sets Cz's default value for "port" to 4000
config.defaults({
    port: 4000
});

// Sets Cz to { random: 'random value' }
config.set('random', 'random value');

// This will return the whole config object currently loaded
console.log('Current config:', config.get());

// This will get { host: 'localhost', port: 27017, username: 'user', password: 'password', collection: 'my-app' } from the config object
console.log('db config:', config.get('db'));
console.log('db host:', config.get('db:host'));

// Example of how to construct a connection string
const mongoUri = 'mongodb://' + config.get('db:username') + ':' + config.get('db:password') + '@' + config.get('db:host') + ':' + config.get('db:port') + '/' + config.get('db:collection');
console.log('MongoDB URI (manual):', mongoUri);

// Cz provides a helper to join gets for you
const mongoUriJoined = 'mongodb://' + config.joinGets(['db:username', 'db:password', 'db:host', 'db:port', 'db:collection'], [':', '@', ':', '/']);
console.log('MongoDB URI (joined):', mongoUriJoined);

view raw JSON →