Configuration via ConfigModule, not `process.env`
Inject ConfigService — services never read `process.env` directly.
nestjs-config-module-not-process-env
Why it matters
| Stake | If ignored |
|---|---|
| Hard to test |
|
| Drops connection |
|
How to fix
NestJS provides @nestjs/config with schema validation. Use it. Services receive an injected ConfigService — they don't read process.env directly. (This is the NestJS expression of rule 16.)
Examples
ts
@Injectable()
export class LlmService {
private client = new LlmClient({
apiKey: process.env.LLM_API_KEY!, // ← if missing, runtime crash
});
}ts
// apps/api/src/config/config.schema.ts
import { z } from 'zod';
export const ConfigSchema = z.object({
LLM_API_KEY: z.string().min(1),
DATABASE_URL: z.string().url(),
PORT: z.coerce.number().default(3000),
});
// apps/api/src/main.ts
ConfigModule.forRoot({
validate: (env) => ConfigSchema.parse(env),
isGlobal: true,
});
// libs/workspace/backend-feature/src/lib/llm.service.ts
@Injectable()
export class LlmService {
private client: LlmClient;
constructor(config: ConfigService) {
this.client = new LlmClient({ apiKey: config.get('LLM_API_KEY') });
}
}