or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

async-config.mdconfig-utilities.mdcore-config.mddeferred-config.mdenvironment-integration.mdindex.mdraw-config.md
tile.json

environment-integration.mddocs/

Environment Integration

Comprehensive environment variable support, command line parameter handling, and multi-environment configuration loading. Provides flexible configuration loading based on deployment environments, hostnames, and runtime parameters.

Capabilities

Configuration File Loading

Automatic loading of configuration files based on environment variables and deployment context.

// Configuration files are loaded in this order:
// 1. default.EXT
// 2. {NODE_ENV}.EXT  
// 3. {hostname}.EXT
// 4. {hostname}-{NODE_ENV}.EXT
// 5. local.EXT
// 6. local-{NODE_ENV}.EXT
// 7. {basename}-{NODE_APP_INSTANCE}.EXT (if NODE_APP_INSTANCE is set)

// Supported file extensions (EXT):
type ConfigFileExtension = 
  | 'js'         // JavaScript module.exports
  | 'ts'         // TypeScript module.exports
  | 'json'       // JSON format
  | 'json5'      // JSON5 format (relaxed JSON)
  | 'jsonc'      // JSON with comments
  | 'hjson'      // Human JSON
  | 'yaml'       // YAML format
  | 'yml'        // YAML format (short extension)
  | 'coffee'     // CoffeeScript
  | 'iced'       // Iced CoffeeScript
  | 'cson'       // CoffeeScript Object Notation
  | 'properties' // Java-style properties
  | 'toml'       // TOML format
  | 'xml';       // XML format

Usage Examples:

// File loading examples based on environment:
// NODE_ENV=production, hostname=web-server-01, NODE_APP_INSTANCE=worker-1

// Files loaded in order (if they exist):
// 1. config/default.json
// 2. config/production.json  
// 3. config/web-server-01.json
// 4. config/web-server-01-production.json
// 5. config/local.json
// 6. config/local-production.json
// 7. config/default-worker-1.json
// 8. config/production-worker-1.json
// 9. config/web-server-01-worker-1.json
// 10. config/web-server-01-production-worker-1.json
// 11. config/local-worker-1.json
// 12. config/local-production-worker-1.json

// Later files override earlier files (deep merge)

Environment Variables

Configuration through environment variables with multiple override mechanisms.

// Key environment variables that control config behavior:

interface ConfigEnvironmentVariables {
  NODE_ENV: string;           // Deployment environment (default: 'development')
  NODE_CONFIG_ENV: string;    // Override NODE_ENV for config purposes
  NODE_CONFIG: string;        // JSON string to override configurations
  NODE_APP_INSTANCE: string;  // Application instance identifier
  NODE_CONFIG_DIR: string;    // Custom configuration directory path
  CONFIG_DIR: string;         // Alternative config directory variable
  HOST: string;               // Hostname for host-specific configs
  HOSTNAME: string;           // Alternative hostname variable
  
  // Parser and file handling:
  NODE_CONFIG_PARSER: string;          // Custom parser module path
  CONFIG_SKIP_GITCRYPT: string;        // Skip git-crypt encrypted files
  
  // Control flags:
  ALLOW_CONFIG_MUTATIONS: string;      // Allow config modifications after first access
  SUPPRESS_NO_CONFIG_WARNING: string;  // Suppress warnings when no config files found
  SUPPRESS_STRICTNESS_CHECK: string;   // Suppress strictness validation
  NODE_CONFIG_STRICT_MODE: string;     // Enable strict validation mode
}

Usage Examples:

// Setting environment variables
process.env.NODE_ENV = 'production';
process.env.NODE_CONFIG_DIR = './config';
process.env.NODE_APP_INSTANCE = 'worker-1';

// JSON configuration override
process.env.NODE_CONFIG = JSON.stringify({
  database: {
    host: 'prod-db.example.com',
    port: 5432
  },
  logging: {
    level: 'info'
  }
});

// Host-specific configuration
process.env.HOSTNAME = 'web-server-01.production.example.com';
// Hostname is shortened to 'web-server-01' for config file matching

const config = require('config');
console.log('Environment:', config.util.getEnv('NODE_ENV'));
console.log('Config dir:', config.util.getEnv('CONFIG_DIR'));

Command Line Parameter Support

Override configurations via command line arguments.

/**
 * Get command line argument value
 * @param searchFor - Argument name to search for
 * @returns Argument value or false if not found
 */
config.util.getCmdLineArg(searchFor: string): any;

/**
 * Initialize parameter from command line, environment, or default
 * @param paramName - Parameter name to initialize
 * @param defaultValue - Default value if not found
 * @returns Resolved parameter value
 */
config.util.initParam(paramName: string, defaultValue?: any): any;

Usage Examples:

// Command line usage:
// node app.js --NODE_ENV=production --CONFIG_DIR=./prod-config --NODE_CONFIG='{"debug":false}'

const config = require('config');

// Get specific command line arguments
const nodeEnv = config.util.getCmdLineArg('NODE_ENV');
const configDir = config.util.getCmdLineArg('CONFIG_DIR');
const nodeConfig = config.util.getCmdLineArg('NODE_CONFIG');

console.log('Command line NODE_ENV:', nodeEnv);

// Initialize parameters with fallbacks
const environment = config.util.initParam('NODE_ENV', 'development');
const port = config.util.initParam('PORT', 3000);
const debugMode = config.util.initParam('DEBUG', false);

// Parameter resolution order:
// 1. Command line argument (--PARAM=value)
// 2. Environment variable (process.env.PARAM)
// 3. Default value

// JSON override from command line
if (nodeConfig) {
  try {
    const overrides = JSON.parse(nodeConfig);
    console.log('Config overrides:', overrides);
  } catch (error) {
    console.error('Invalid JSON in --NODE_CONFIG argument');
  }
}

Custom Environment Variable Mapping

Map environment variables to configuration properties using custom mapping files.

Usage Examples:

// Create config/custom-environment-variables.json
{
  "database": {
    "host": "DB_HOST",
    "port": "DB_PORT",
    "username": "DB_USER",
    "password": "DB_PASS"
  },
  "api": {
    "key": "API_KEY",
    "timeout": "API_TIMEOUT"
  },
  "server": {
    "port": "PORT"
  }
}

// Set environment variables
process.env.DB_HOST = 'db.production.com';
process.env.DB_PORT = '5432';
process.env.DB_USER = 'produser';
process.env.DB_PASS = 'secret123';
process.env.API_KEY = 'abc123';
process.env.PORT = '8080';

const config = require('config');

// Environment variables are mapped to config properties
console.log('Database host:', config.get('database.host')); // 'db.production.com'
console.log('Database port:', config.get('database.port')); // '5432'
console.log('API key:', config.get('api.key')); // 'abc123'
console.log('Server port:', config.get('server.port')); // '8080'

// Access custom environment variable mappings
const customEnvVars = config.util.getCustomEnvVars('./config', ['json']);
console.log('Custom mappings:', customEnvVars);

Multi-Instance Application Support

Configuration support for running multiple instances of the same application.

Usage Examples:

// Set application instance
process.env.NODE_APP_INSTANCE = 'worker-1';

// Additional config files loaded:
// - default-worker-1.json
// - production-worker-1.json (if NODE_ENV=production)
// - hostname-worker-1.json
// - hostname-production-worker-1.json
// - local-worker-1.json
// - local-production-worker-1.json

// Example config/default-worker-1.json
{
  "server": {
    "port": 3001,
    "name": "worker-1"
  },
  "worker": {
    "id": 1,
    "queues": ["high-priority", "normal"],
    "concurrency": 5
  }
}

// Example config/default-worker-2.json  
{
  "server": {
    "port": 3002,
    "name": "worker-2"
  },
  "worker": {
    "id": 2,
    "queues": ["normal", "low-priority"],
    "concurrency": 3
  }
}

const config = require('config');

// Instance-specific configuration
console.log('Worker ID:', config.get('worker.id'));
console.log('Server port:', config.get('server.port'));
console.log('Queues:', config.get('worker.queues'));

// Starting multiple instances:
// NODE_APP_INSTANCE=worker-1 node app.js
// NODE_APP_INSTANCE=worker-2 node app.js

Configuration Directory Management

Flexible configuration directory location and multiple directory support.

Usage Examples:

// Single configuration directory
process.env.NODE_CONFIG_DIR = './config';

// Multiple configuration directories (colon-separated)
process.env.NODE_CONFIG_DIR = './config:./shared-config:./local-config';

// Files are searched in order across all directories
// Later directories can override files from earlier directories

const config = require('config');

// Get current configuration directory
const configDir = config.util.getEnv('CONFIG_DIR');
console.log('Config directory:', configDir);

// Load from custom directory
const customConfig = config.util.loadFileConfigs('./custom-config');

// Load with multiple directories
const multiConfig = config.util.loadFileConfigs('./config:./overrides');

Hostname-Based Configuration

Automatic hostname detection and configuration loading based on server hostname.

Usage Examples:

// Hostname detection order:
// 1. HOST environment variable
// 2. HOSTNAME environment variable  
// 3. require('os').hostname()

// Hostname processing:
// Full hostname: web-server-01.production.example.com
// Config hostname: web-server-01 (everything after first dot removed)

process.env.HOSTNAME = 'web-server-01.production.example.com';

const config = require('config');

// Configuration files loaded for hostname:
// - web-server-01.json
// - web-server-01-production.json (if NODE_ENV=production)

// Check current hostname
const hostname = require('os').hostname();
console.log('System hostname:', hostname);

// Override hostname for config
process.env.HOST = 'custom-host';

// Hostname-specific configuration example
// config/web-server-01.json
{
  "server": {
    "host": "0.0.0.0",
    "port": 8080,
    "workers": 4
  },
  "database": {
    "pool": {
      "min": 5,
      "max": 20
    }
  }
}

// config/web-server-02.json  
{
  "server": {
    "host": "0.0.0.0", 
    "port": 8080,
    "workers": 8
  },
  "database": {
    "pool": {
      "min": 10,
      "max": 40
    }
  }
}

Strictness and Validation

Configuration validation and strictness checking to catch common deployment issues.

// Strictness checks validate:
// - NODE_ENV matches at least one configuration file
// - NODE_ENV is not 'default' or 'local' (ambiguous values)
// - NODE_APP_INSTANCE matches at least one configuration file (if set)

interface StrictnessOptions {
  SUPPRESS_STRICTNESS_CHECK: boolean;  // Disable all strictness checks
  NODE_CONFIG_STRICT_MODE: boolean;    // Throw errors instead of warnings
}

Usage Examples:

// Enable strict mode
process.env.NODE_CONFIG_STRICT_MODE = 'true';

// This will throw an error if NODE_ENV doesn't match any config files
process.env.NODE_ENV = 'nonexistent-environment';

try {
  const config = require('config');
} catch (error) {
  console.error('Strict mode error:', error.message);
  // "FATAL: nonexistent-environment value of 'nonexistent-environment' did not match any deployment config file names."
}

// Suppress strictness checks
process.env.SUPPRESS_STRICTNESS_CHECK = 'true';

// Warning mode (default)
process.env.NODE_CONFIG_STRICT_MODE = 'false';
process.env.NODE_ENV = 'nonexistent-environment';

const config = require('config');
// Logs warning: "WARNING: nonexistent-environment value of 'nonexistent-environment' did not match any deployment config file names."

// Suppress no-config warning
process.env.SUPPRESS_NO_CONFIG_WARNING = 'true';

// This prevents warning when no config files are found