Always implement proper fallback chains for environment variables and handle missing configuration gracefully. When introducing new environment variables, maintain backward compatibility with existing ones, and apply environment-specific behavior conditionally to avoid issues across different deployment contexts.
Always implement proper fallback chains for environment variables and handle missing configuration gracefully. When introducing new environment variables, maintain backward compatibility with existing ones, and apply environment-specific behavior conditionally to avoid issues across different deployment contexts.
Key practices:
IS_DOCKER_HOSTED
)Example implementation:
export function resolveLogging(providedLogging?: boolean): boolean {
if (providedLogging !== undefined) {
return providedLogging;
}
// Support both new and legacy environment variables
if (process.env.LOGGING_LEVEL || process.env.LOG_LEVEL) {
return (process.env.LOGGING_LEVEL || process.env.LOG_LEVEL) === 'true';
}
// Graceful fallback based on environment
return !['test', 'production'].includes(process.env.NODE_ENV);
}
// Conditional behavior based on deployment context
const options = process.env.IS_DOCKER_HOSTED === 'true'
? { priority: command.priority }
: {};
For optional services, avoid initialization entirely when configuration is missing rather than using empty defaults, allowing the application to bootstrap gracefully without the optional functionality.
Enter the URL of a public GitHub repository