Bird
Raised Fist0
NestJSframework~10 mins

Why configuration management matters in NestJS - Test Your Understanding

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the ConfigModule in a NestJS application.

NestJS
import { Module } from '@nestjs/common';
import { [1] } from '@nestjs/config';

@Module({
  imports: [ConfigModule.forRoot()],
})
export class AppModule {}
Drag options to blanks, or click blank then click option'
ADatabaseModule
BHttpModule
CAuthModule
DConfigModule
Attempts:
3 left
💡 Hint
Common Mistakes
Importing unrelated modules like HttpModule instead of ConfigModule.
Forgetting to import ConfigModule causes configuration to not load.
2fill in blank
medium

Complete the code to access a configuration value using ConfigService.

NestJS
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';

@Injectable()
export class AppService {
  constructor(private readonly configService: ConfigService) {}

  getDatabaseHost(): string {
    return this.configService.[1]('DATABASE_HOST');
  }
}
Drag options to blanks, or click blank then click option'
Aget
Bset
Cload
Dfetch
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'set' instead of 'get' which causes errors.
Trying to access config values directly without ConfigService.
3fill in blank
hard

Fix the error in the code to load environment variables from a custom path.

NestJS
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';

@Module({
  imports: [
    ConfigModule.forRoot({
      envFilePath: [1],
    }),
  ],
})
export class AppModule {}
Drag options to blanks, or click blank then click option'
Aenv.production
B'.env.production'
C'env.production'
D.env.production
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting quotes causes syntax errors.
Using variable names without quotes instead of string paths.
4fill in blank
hard

Fill both blanks to create a configuration object with a default value and type safety.

NestJS
import { registerAs } from '@nestjs/config';

export default registerAs('database', () => ({
  host: process.env.DB_HOST || [1],
  port: Number(process.env.DB_PORT) || [2],
}));
Drag options to blanks, or click blank then click option'
A'localhost'
B5432
C3306
D'127.0.0.1'
Attempts:
3 left
💡 Hint
Common Mistakes
Using port as a string instead of a number.
Forgetting to provide default values causing undefined errors.
5fill in blank
hard

Fill all three blanks to create a typed configuration interface and use it in a service.

NestJS
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';

interface AppConfig {
  apiKey: string;
  timeout: number;
}

@Injectable()
export class ApiService {
  constructor(private configService: ConfigService) {}

  getApiKey(): string {
    return this.configService.[1]<[2]>('API_KEY');
  }

  getTimeout(): number {
    return this.configService.get<[3]>('TIMEOUT');
  }
}
Drag options to blanks, or click blank then click option'
Aget
BAppConfig
Cnumber
Dfetch
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'fetch' instead of 'get' causes errors.
Not specifying generic types loses type safety.

Practice

(1/5)
1. Why is configuration management important in a NestJS application?
easy
A. It keeps app settings organized and allows changes without modifying code
B. It automatically writes all application code for you
C. It replaces the need for a database
D. It makes the app run faster by skipping validations

Solution

  1. Step 1: Understand the role of configuration management

    Configuration management organizes app settings and separates them from code.
  2. Step 2: Identify benefits in NestJS context

    It allows changing settings like API keys or ports without changing the source code.
  3. Final Answer:

    It keeps app settings organized and allows changes without modifying code -> Option A
  4. Quick Check:

    Config management = organized settings, no code change needed [OK]
Hint: Config management means settings separate from code [OK]
Common Mistakes:
  • Thinking config management writes code automatically
  • Confusing config with database functionality
  • Assuming config management speeds up app runtime
2. Which of the following is the correct way to import the ConfigModule in a NestJS module?
easy
A. import { ConfigService } from '@nestjs/config'; @Module({ imports: [ConfigService.forRoot()] })
B. import { ConfigModule } from '@nestjs/config'; @Module({ imports: [ConfigModule.forRoot()] })
C. import { ConfigModule } from '@nestjs/config'; @Module({ providers: [ConfigModule.forRoot()] })
D. import { ConfigModule } from '@nestjs/config'; @Module({ exports: [ConfigModule.forRoot()] })

Solution

  1. Step 1: Recall correct import and usage of ConfigModule

    ConfigModule is imported from '@nestjs/config' and initialized with forRoot() inside imports array.
  2. Step 2: Check each option's correctness

    import { ConfigModule } from '@nestjs/config'; @Module({ imports: [ConfigModule.forRoot()] }) correctly imports ConfigModule and uses it in imports with forRoot(). Others misuse ConfigService or wrong module properties.
  3. Final Answer:

    import { ConfigModule } from '@nestjs/config'; @Module({ imports: [ConfigModule.forRoot()] }) -> Option B
  4. Quick Check:

    ConfigModule.forRoot() goes in imports [OK]
Hint: ConfigModule.forRoot() always goes inside imports [OK]
Common Mistakes:
  • Using ConfigService in imports instead of ConfigModule
  • Placing forRoot() inside providers or exports
  • Forgetting to import ConfigModule from '@nestjs/config'
3. Given this NestJS service code snippet, what will be the output when calling getDatabaseHost() if the environment variable DATABASE_HOST is set to 'db.example.com'?
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';

@Injectable()
export class DatabaseService {
  constructor(private configService: ConfigService) {}

  getDatabaseHost(): string {
    return this.configService.get<string>('DATABASE_HOST', 'localhost');
  }
}
medium
A. 'db.example.com'
B. undefined
C. 'localhost'
D. Throws an error

Solution

  1. Step 1: Understand ConfigService.get behavior

    ConfigService.get returns the environment variable value if set; otherwise, it returns the default value.
  2. Step 2: Apply to given environment variable

    Since DATABASE_HOST is set to 'db.example.com', get returns this value, not the default 'localhost'.
  3. Final Answer:

    'db.example.com' -> Option A
  4. Quick Check:

    Env var set, so get returns 'db.example.com' [OK]
Hint: ConfigService.get returns env var if set, else default [OK]
Common Mistakes:
  • Assuming default value is always returned
  • Thinking it returns undefined if env var exists
  • Expecting an error if env var is missing
4. You wrote this code to use ConfigService in a NestJS service, but you get a runtime error: "Nest can't resolve dependencies of the DatabaseService". What is the likely cause?
@Injectable()
export class DatabaseService {
  constructor(private configService: ConfigService) {}

  getPort(): number {
    return this.configService.get<number>('PORT');
  }
}
Options:
medium
A. ConfigService is not decorated with @Injectable()
B. DatabaseService must extend ConfigService
C. The get method of ConfigService requires a default value
D. ConfigModule was not imported in the module where DatabaseService is declared

Solution

  1. Step 1: Understand dependency injection in NestJS

    Services like ConfigService must be provided by importing their module (ConfigModule) in the module declaring DatabaseService.
  2. Step 2: Identify missing import causing error

    If ConfigModule is not imported, NestJS cannot inject ConfigService, causing the dependency resolution error.
  3. Final Answer:

    ConfigModule was not imported in the module where DatabaseService is declared -> Option D
  4. Quick Check:

    Missing ConfigModule import causes injection error [OK]
Hint: Always import ConfigModule to use ConfigService [OK]
Common Mistakes:
  • Forgetting to import ConfigModule in the module
  • Thinking ConfigService needs @Injectable() (it already has)
  • Assuming get() must have default value
  • Believing service inheritance is required
5. You want to manage multiple environment configurations (development, testing, production) in NestJS using ConfigModule. Which approach best supports this without changing code when switching environments?
hard
A. Manually edit the source code to change config values for each environment
B. Hardcode all environment variables inside ConfigService
C. Use ConfigModule.forRoot({ envFilePath: `.env.${process.env.NODE_ENV}` }) to load different .env files
D. Use a single .env file and comment/uncomment variables for each environment

Solution

  1. Step 1: Understand environment-specific config loading

    ConfigModule can load different .env files based on NODE_ENV, allowing environment-specific settings without code changes.
  2. Step 2: Evaluate options for best practice

    Use ConfigModule.forRoot({ envFilePath: `.env.${process.env.NODE_ENV}` }) to load different .env files uses envFilePath with process.env.NODE_ENV to select the correct .env file automatically. Others require manual code or file edits, which are error-prone.
  3. Final Answer:

    Use ConfigModule.forRoot({ envFilePath: `.env.${process.env.NODE_ENV}` }) to load different .env files -> Option C
  4. Quick Check:

    Dynamic envFilePath loads correct config per environment [OK]
Hint: Use envFilePath with NODE_ENV for environment configs [OK]
Common Mistakes:
  • Hardcoding config values inside code
  • Manually changing code for each environment
  • Using one .env file with manual edits
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard