Core configuration access providing the primary interface for retrieving and validating configuration values. Includes both method-based and direct property access with full validation support.
Primary methods for accessing configuration values with validation and error handling.
/**
* Get configuration value by property path
* @param property - Dot-notation path to configuration property (e.g., 'database.host')
* @returns Configuration value of any type
* @throws Error if property is null, undefined, or doesn't exist
*/
config.get(property: string): any;
/**
* Test if configuration property exists
* @param property - Dot-notation path to configuration property
* @returns True if property is defined, false otherwise
*/
config.has(property: string): boolean;Usage Examples:
const config = require('config');
// Basic property access
const dbHost = config.get('database.host');
const port = config.get('server.port');
// Nested property access
const credentials = config.get('services.api.credentials');
// Check existence before access
if (config.has('features.experimental')) {
const experimental = config.get('features.experimental');
console.log('Experimental features:', experimental);
}
// Error handling
try {
const value = config.get('nonexistent.property');
} catch (error) {
console.error('Configuration property not found:', error.message);
}Direct access to configuration properties as object properties. All configuration objects inherit Config prototype methods.
// Direct property access (alternative to get() method)
config.propertyName: any;
config.nested.property: any;
// All configuration sub-objects have Config methods
config.database.get(property: string): any;
config.database.has(property: string): boolean;Usage Examples:
const config = require('config');
// Direct property access
const appName = config.appName;
const dbConfig = config.database;
// Sub-configuration access
const dbHost = config.database.host;
const dbPort = config.database.port;
// Using Config methods on sub-objects
if (config.database.has('credentials')) {
const credentials = config.database.get('credentials');
}
// Mixed access patterns
const serverConfig = config.get('server');
const serverHost = serverConfig.host; // Direct access on returned object
const serverPort = serverConfig.get('port'); // Method access on returned objectConfiguration values can be of any JavaScript type, with full support for nested objects and arrays.
// Supported configuration value types
type ConfigValue =
| string
| number
| boolean
| null
| object
| any[]
| ConfigValue[];
// Configuration objects maintain type information
interface ConfigObject {
[key: string]: ConfigValue;
get(property: string): ConfigValue;
has(property: string): boolean;
}Usage Examples:
// String values
const appName = config.get('app.name'); // "My Application"
// Number values
const port = config.get('server.port'); // 3000
const timeout = config.get('timeout'); // 30.5
// Boolean values
const debugMode = config.get('debug'); // true
// Object values
const dbConfig = config.get('database');
// { host: 'localhost', port: 5432, name: 'myapp' }
// Array values
const allowedHosts = config.get('security.allowedHosts');
// ['localhost', '127.0.0.1', 'myapp.com']
// Nested arrays and objects
const services = config.get('services');
// { api: { url: '...', timeout: 5000 }, cache: { enabled: true } }Configuration access includes comprehensive error handling for undefined properties and invalid access patterns.
/**
* Configuration errors thrown by get() method
*/
class ConfigError extends Error {
message: string; // Description of the configuration error
}
// Error conditions:
// - get(null) or get(undefined) throws: "Calling config.get with null or undefined argument"
// - get('nonexistent.property') throws: 'Configuration property "nonexistent.property" is not defined'Usage Examples:
const config = require('config');
// Error handling patterns
try {
const value = config.get('missing.property');
} catch (error) {
if (error.message.includes('is not defined')) {
console.log('Property not found, using default');
const value = 'default-value';
}
}
// Safe access with has() check
function getConfigSafely(property, defaultValue) {
return config.has(property) ? config.get(property) : defaultValue;
}
const dbTimeout = getConfigSafely('database.timeout', 5000);
// Validate arguments before calling get()
function getConfigValue(property) {
if (property == null) {
throw new Error('Property path cannot be null or undefined');
}
return config.get(property);
}