Skip to content
Latchkey

NestJS ConfigModule env var undefined in CI

ConfigService returned undefined because the value it expected came from a .env file that is not committed, and the CI runner has no equivalent environment variable set. Config validation or a downstream provider then fails.

What this error means

The app throws when reading a config value, or validation fails with "Config validation error: X is required", because the variable is absent on the runner.

NestJS
Error: Config validation error: "DATABASE_URL" is required
    at validate (/app/dist/config/env.validation.js:...)

Common causes

The .env file is not present in CI

.env is gitignored (correctly), so on the runner the values never load and ConfigService reads undefined.

Variables are not injected into the job environment

No env: block or secrets were mapped to the step, so process.env has none of the expected keys.

How to fix it

Provide env vars via the workflow

  1. Map required values into the job or step env: from secrets.
  2. Keep a validation schema so missing keys fail loudly at startup.
  3. Do not rely on a committed .env for CI secrets.
.github/workflows/ci.yml
env:
  DATABASE_URL: ${{ secrets.DATABASE_URL }}
  JWT_SECRET: ${{ secrets.JWT_SECRET }}

Validate config at bootstrap

Use ConfigModule validation so a missing variable fails fast with a clear message instead of undefined later.

app.module.ts
ConfigModule.forRoot({ validationSchema: envSchema, isGlobal: true })

How to prevent it

  • Inject all required config via workflow env or secrets, never a committed .env.
  • Add a validation schema so missing variables fail at startup.
  • Document required variables so CI and local stay aligned.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →