Performance: Environment-based configuration
MEDIUM IMPACT
This concept affects the app's startup time and runtime efficiency by managing configuration loading and conditional code execution.
import { Module } from '@nestjs/common'; import { ConfigModule, ConfigService } from '@nestjs/config'; @Module({ imports: [ConfigModule.forRoot({ isGlobal: true, envFilePath: `.env.${process.env.NODE_ENV}` })], }) export class AppModule {} // Inject ConfigService where needed and cache values on init
import * as dotenv from 'dotenv'; dotenv.config(); const config = { dbHost: process.env.DB_HOST, dbPort: Number(process.env.DB_PORT), featureFlag: process.env.FEATURE_FLAG === 'true', }; // Use config directly everywhere without caching or environment checks
| Pattern | Config Loading | Startup Blocking | Runtime Overhead | Verdict |
|---|---|---|---|---|
| Direct dotenv.config() and process.env access | Loads all env vars at once | Blocks startup until .env parsed | Repeated env var reads slow runtime | [X] Bad |
| NestJS ConfigModule with envFilePath per environment | Loads only needed env file once | Minimal startup blocking | Cached config access at runtime | [OK] Good |