Performance: Environment variables
Environment variables affect application startup time and configuration loading, impacting initial page load speed and server response readiness.
import { Module } from '@nestjs/common'; import { ConfigModule } from '@nestjs/config'; @Module({ imports: [ConfigModule.forRoot({ isGlobal: true })], }) export class AppModule {}
import * as dotenv from 'dotenv'; dotenv.config({ path: './.env' }); // Access process.env directly throughout the app
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Synchronous dotenv.config() on every import | N/A | N/A | Delays server response, increasing LCP | [X] Bad |
| NestJS ConfigModule with global loading | N/A | N/A | Fast server startup, minimal LCP impact | [OK] Good |
constructor(private configService: ConfigService) {}
getDbHost() {
return this.configService.get('DATABASE_HOST');
}DATABASE_HOST=localhost, what will getDbHost() return?@Module({
imports: [ConfigModule],
})
export class AppModule {}PORT in NestJS if it is not set in the .env file. Which code snippet correctly achieves this in a service?Pass the test and this pattern moves unknown