Complete the code to import the ConfigModule in a NestJS module.
import { Module } from '@nestjs/common'; import { ConfigModule } from '@nestjs/config'; @Module({ imports: [ConfigModule.[1]()], }) export class AppModule {}
The ConfigModule.forRoot() method loads environment variables and makes them available throughout the app.
Complete the code to inject ConfigService into a service constructor.
import { Injectable } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; @Injectable() export class AppService { constructor(private readonly [1]: ConfigService) {} }
The common convention is to name the injected ConfigService instance configService for clarity.
Fix the error in accessing an environment variable using ConfigService.
const port = this.configService.[1]('PORT');
The correct method to get environment variables from ConfigService is get.
Fill in the blank to access an environment variable with a default value fallback.
const dbHost = this.configService.[1]('DATABASE_HOST', 'localhost');
The get method accepts a second argument as a default value if the environment variable is not set.
Fill all three blanks to define a custom configuration loader function.
import { [1] } from '@nestjs/config'; export const customConfig = registerAs([2], () => ({ port: parseInt(process.env.[3] || '3000', 10), }));
registerAs is used to create a named configuration. The function returns an object with the port read from environment variable PORT.