Skip to content

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

Failure modes if this rule is ignored
StakeIf ignored
Drops connection
  • Env vars scattered in services — missing key crashes prod an hour after deploy, not at boot.
  • Validation at startup — if a var is missing, the app doesn't boot. Better than crashing an hour later.
Hard to test
  • Tests receive config as a prop, not by stubbing process.env.

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

Bad
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');
Good
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';

Contribute

Released under the MIT License.

esc