Read environment variables with a fallback. Avoids undefined in Node or edge runtimes.
export function readEnv(key: string, fallback = ''): string {
if (typeof process === 'undefined' || process.env == null) return fallback;
const value = process.env[key];
return value !== undefined && value !== '' ? value : fallback;
}Use in Node.js or Edge. Avoids undefined when env var is missing.
readEnv('DATABASE_URL', 'sqlite://local') returns the env value or the fallback. Good for required config with a default.
const apiKey = readEnv('API_KEY');