From b18ee8d7a0eebe0f3f19f4dd6648d5cecf98e691 Mon Sep 17 00:00:00 2001 From: Marc Mintel Date: Wed, 28 Jan 2026 00:02:24 +0100 Subject: [PATCH] envs --- lib/config.ts | 27 ++++++++++++++++++-------- lib/services/create-services.server.ts | 7 +++++++ lib/services/create-services.ts | 7 +++++++ 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/lib/config.ts b/lib/config.ts index 76c9c381..3db7f0db 100644 --- a/lib/config.ts +++ b/lib/config.ts @@ -2,17 +2,28 @@ * Centralized configuration management for the application. * This file defines the schema and provides a type-safe way to access environment variables. */ -import dotenv from 'dotenv'; -import path from 'path'; - -// Load .env file in development or if not already loaded -if (typeof process !== 'undefined' && process.env.NODE_ENV !== 'production') { - dotenv.config({ path: path.resolve(process.cwd(), '.env') }); -} const getEnv = (key: string, defaultValue?: string): string | undefined => { + // In the browser, we can only access NEXT_PUBLIC_ variables + if (typeof window !== 'undefined') { + if (!key.startsWith('NEXT_PUBLIC_')) { + return defaultValue; + } + return (process.env as any)[key] || defaultValue; + } + if (typeof process === 'undefined') return defaultValue; - return process.env[key] || defaultValue; + if (typeof process === 'undefined') return defaultValue; + + // In Docker/Production, variables are in process.env + // In local development, they might be in .env + const value = process.env[key]; + + if (value !== undefined && value !== '') { + return value; + } + + return defaultValue; }; export const config = { diff --git a/lib/services/create-services.server.ts b/lib/services/create-services.server.ts index 83db3bc7..d6d758e9 100644 --- a/lib/services/create-services.server.ts +++ b/lib/services/create-services.server.ts @@ -7,6 +7,13 @@ import { GlitchtipErrorReportingService } from './errors/glitchtip-error-reporti import { NoopErrorReportingService } from './errors/noop-error-reporting-service'; import { PinoLoggerService } from './logging/pino-logger-service'; import { config, getMaskedConfig } from '../config'; +import dotenv from 'dotenv'; +import path from 'path'; + +// Load .env file in development +if (process.env.NODE_ENV !== 'production') { + dotenv.config({ path: path.resolve(process.cwd(), '.env') }); +} let singleton: AppServices | undefined; diff --git a/lib/services/create-services.ts b/lib/services/create-services.ts index f3982bfc..7032ff06 100644 --- a/lib/services/create-services.ts +++ b/lib/services/create-services.ts @@ -6,6 +6,13 @@ import { NoopErrorReportingService } from './errors/noop-error-reporting-service import { NoopLoggerService } from './logging/noop-logger-service'; import { PinoLoggerService } from './logging/pino-logger-service'; import { config, getMaskedConfig } from '../config'; +import dotenv from 'dotenv'; +import path from 'path'; + +// Load .env file in development +if (typeof window === 'undefined' && process.env.NODE_ENV !== 'production') { + dotenv.config({ path: path.resolve(process.cwd(), '.env') }); +} /** * Singleton instance of AppServices.