Configuration is read at the entry point, not in the middle of the code
No surprises. All settings in one reviewable place.
nx-monorepo-schema-config-at-entry
Why it matters
| Stake | If ignored |
|---|---|
| Drops connection |
|
| Hard to test |
|
How to fix
process.env.X or import.meta.env.X appears in one place — the app's config file. The rest of the code receives typed values, doesn't read env vars directly.
Examples
ts
// scattered everywhere
const apiKey = process.env.OPENAI_API_KEY;
const dbUrl = process.env.DATABASE_URL ?? 'postgres://localhost';
const timeout = parseInt(process.env.TIMEOUT_MS ?? '5000');ts
// apps/workspace/src/config.ts
import { z } from 'zod';
const ConfigSchema = z.object({
llmApiKey: z.string().min(1),
databaseUrl: z.string().url(),
timeoutMs: z.coerce.number().default(5000),
});
export const config = ConfigSchema.parse({
llmApiKey: process.env.LLM_API_KEY,
databaseUrl: process.env.DATABASE_URL,
timeoutMs: process.env.TIMEOUT_MS,
});
// everywhere else
import { config } from './config';