Configuration control for production node deployments
npx @tessl/cli install tessl/npm-config@4.1.0Config is a comprehensive configuration management library for Node.js applications that enables hierarchical configurations across different deployment environments. It supports multiple configuration file formats, environment variable overrides, command line parameters, and provides a consistent interface for accessing configuration values with validation and type safety.
npm install configconst config = require('config');For ES modules (if supported):
import config from 'config';Access to additional modules:
const { asyncConfig, resolveAsyncConfigs } = require('config/async');
const { deferConfig } = require('config/defer');
const { raw } = require('config/raw');const config = require('config');
// Get configuration values
const dbHost = config.get('database.host');
const dbPort = config.get('database.port');
// Check if configuration exists
if (config.has('cache.enabled')) {
const cacheEnabled = config.get('cache.enabled');
}
// Access nested configuration
const serverConfig = config.get('server');
console.log(`Server: ${serverConfig.host}:${serverConfig.port}`);
// Direct property access (also available)
const appName = config.appName;Config is built around several key components:
Primary interface for accessing configuration values with validation and type safety. Provides get/has methods and direct property access.
/**
* Get configuration value by property path
* @param property - Dot-notation path to configuration property
* @returns Configuration value
* @throws Error if property is undefined
*/
config.get(property: string): any;
/**
* Test if configuration property exists
* @param property - Dot-notation path to configuration property
* @returns True if property exists, false otherwise
*/
config.has(property: string): boolean;Comprehensive utilities for configuration management, including module defaults, immutability control, file loading, and object manipulation.
// Module defaults
config.util.setModuleDefaults(moduleName: string, defaultProperties: object): object;
// Immutability control
config.util.makeImmutable(object: object, property?: string | string[], value?: any): object;
config.util.makeHidden(object: object, property: string, value?: any): object;
// Configuration sources and loading
config.util.getConfigSources(): ConfigSource[];
config.util.loadFileConfigs(configDir?: string, options?: LoadOptions): object;Support for asynchronous configuration loading with promise-based resolution and deferred initialization.
/**
* Create async configuration value
* @param promiseOrFunc - Promise or function returning promise
* @returns Async configuration object
*/
function asyncConfig(promiseOrFunc: Promise | Function): AsyncConfig;
/**
* Resolve all async configurations
* @param config - Configuration object containing async values
* @returns Promise resolving to completed configuration
*/
function resolveAsyncConfigs(config: object): Promise<object>;Lazy-loading configuration values that are computed on first access using provided functions.
/**
* Create deferred configuration value
* @param func - Function to compute value on first access
* @returns Deferred configuration object
*/
function deferConfig(func: Function): DeferredConfig;Protection for configuration objects that should remain unmodified by the config system.
/**
* Wrap object to prevent config system modifications
* @param rawObj - Object to protect from modifications
* @returns Raw configuration wrapper
*/
function raw(rawObj: object): RawConfig;Comprehensive environment variable support, command line parameter handling, and multi-environment configuration loading.
Configuration file loading order:
default.EXT{NODE_ENV}.EXT{hostname}.EXT{hostname}-{NODE_ENV}.EXTlocal.EXTlocal-{NODE_ENV}.EXTSupported file extensions (EXT): js, ts, json, json5, jsonc, hjson, yaml, yml, coffee, iced, cson, properties, toml, xml