Bird
Raised Fist0
NestJSframework~5 mins

ConfigModule setup in NestJS - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is the purpose of the ConfigModule in NestJS?
The ConfigModule helps manage application configuration settings in a centralized and organized way, allowing easy access to environment variables and config values throughout the app.
Click to reveal answer
beginner
How do you import the ConfigModule globally in a NestJS application?
Use ConfigModule.forRoot({ isGlobal: true }) in the root module's imports array. This makes the configuration available in all modules without needing to import ConfigModule again.
Click to reveal answer
beginner
What file does ConfigModule load by default when using forRoot()?
By default, ConfigModule loads the .env file from the project root to read environment variables.
Click to reveal answer
intermediate
How can you validate environment variables when setting up ConfigModule?
You can pass a validationSchema option with a Joi schema to ConfigModule.forRoot() to check environment variables and ensure they meet expected formats.
Click to reveal answer
beginner
How do you access a configuration value inside a service after setting up ConfigModule?
Inject ConfigService in the service constructor, then use configService.get('KEY_NAME') to get the value of a config key.
Click to reveal answer
Which method makes ConfigModule available globally in NestJS?
AConfigModule.forRoot({ isGlobal: true })
BConfigModule.forFeature()
CConfigModule.register()
DConfigModule.useGlobal()
What file does ConfigModule load by default to get environment variables?
Asettings.yaml
Bconfig.json
C.env
Dpackage.json
How do you access a config value inside a NestJS service?
AInject ConfigService and call get('KEY')
BUse process.env.KEY directly
CImport ConfigModule inside the service
DCall ConfigModule.getConfig('KEY')
Which package is commonly used to validate environment variables in ConfigModule?
ALodash
BJoi
CExpress
DAxios
What happens if you don't set isGlobal: true in ConfigModule.forRoot()?
AApplication will crash
BConfigModule will not load .env file
CConfigService will be unavailable
DYou must import ConfigModule in every module that needs it
Explain how to set up ConfigModule globally in a NestJS app and why it is useful.
Think about how to avoid importing ConfigModule repeatedly.
You got /4 concepts.
    Describe how to validate environment variables using ConfigModule in NestJS.
    Validation helps catch config errors early.
    You got /4 concepts.

      Practice

      (1/5)
      1. What is the main purpose of ConfigModule in a NestJS application?
      easy
      A. To manage application settings using environment variables
      B. To handle HTTP requests and responses
      C. To connect to a database
      D. To create middleware for logging

      Solution

      1. Step 1: Understand ConfigModule role

        The ConfigModule is designed to load and manage configuration settings, often from environment variables.
      2. Step 2: Compare with other options

        Options A, B, and D relate to other parts of NestJS like database connections, HTTP handling, and middleware, not configuration management.
      3. Final Answer:

        To manage application settings using environment variables -> Option A
      4. Quick Check:

        ConfigModule = Manage app settings [OK]
      Hint: ConfigModule is for app settings, not HTTP or DB [OK]
      Common Mistakes:
      • Confusing ConfigModule with database modules
      • Thinking ConfigModule handles HTTP requests
      • Assuming ConfigModule creates middleware
      2. Which is the correct way to import ConfigModule globally in the root module?
      easy
      A. ConfigModule.register({ global: true })
      B. ConfigModule.forFeature({ isGlobal: true })
      C. ConfigModule.forRoot({ isGlobal: true })
      D. ConfigModule.global()

      Solution

      1. Step 1: Recall ConfigModule global setup syntax

        To make ConfigModule available app-wide, use forRoot() with isGlobal: true.
      2. Step 2: Check other options for correctness

        forFeature() is for scoped configs, register() and global() are not valid methods here.
      3. Final Answer:

        ConfigModule.forRoot({ isGlobal: true }) -> Option C
      4. Quick Check:

        Global config = forRoot + isGlobal true [OK]
      Hint: Use forRoot with isGlobal true for global config [OK]
      Common Mistakes:
      • Using forFeature instead of forRoot
      • Trying to use non-existent methods like global()
      • Confusing register with forRoot
      3. Given this code snippet in a NestJS service:
      constructor(private configService: ConfigService) {}
      
      getDbHost() {
        return this.configService.get('DATABASE_HOST');
      }

      What will getDbHost() return if DATABASE_HOST is set to "localhost" in the environment?
      medium
      A. Throws an error
      B. undefined
      C. null
      D. "localhost"

      Solution

      1. Step 1: Understand ConfigService.get behavior

        The get method returns the value of the environment variable if set, here 'localhost'.
      2. Step 2: Check environment variable presence

        Since DATABASE_HOST is set to 'localhost', the method returns that string, not undefined or null.
      3. Final Answer:

        "localhost" -> Option D
      4. Quick Check:

        Env var set = get returns value [OK]
      Hint: get() returns env value if set, else undefined [OK]
      Common Mistakes:
      • Assuming get returns null if variable exists
      • Expecting an error if variable is missing
      • Confusing undefined with null
      4. Identify the error in this NestJS module setup:
      @Module({
        imports: [ConfigModule.forRoot({ isGlobal: true })],
        providers: [ConfigService],
      })
      export class AppModule {}
      medium
      A. ConfigService should not be listed in providers when ConfigModule is global
      B. Missing import of ConfigService from '@nestjs/config'
      C. forRoot() should not have isGlobal option
      D. ConfigModule should be imported in controllers, not imports

      Solution

      1. Step 1: Understand global ConfigModule behavior

        When isGlobal: true is set, ConfigService is automatically provided app-wide, no need to add it manually.
      2. Step 2: Check other options for validity

        Importing ConfigService is not mandatory in module file, forRoot supports isGlobal, and ConfigModule belongs in imports array.
      3. Final Answer:

        ConfigService should not be listed in providers when ConfigModule is global -> Option A
      4. Quick Check:

        Global ConfigModule auto-provides ConfigService [OK]
      Hint: Don't add ConfigService to providers if ConfigModule is global [OK]
      Common Mistakes:
      • Adding ConfigService to providers unnecessarily
      • Thinking isGlobal is invalid option
      • Placing ConfigModule in controllers array
      5. You want to load a custom configuration file named custom.env using ConfigModule and make it global. Which setup is correct?
      hard
      A. ConfigModule.forRoot({ envFileName: 'custom.env', global: true })
      B. ConfigModule.forRoot({ envFilePath: 'custom.env', isGlobal: true })
      C. ConfigModule.forFeature({ envFilePath: 'custom.env', isGlobal: true })
      D. ConfigModule.register({ envFilePath: 'custom.env', isGlobal: true })

      Solution

      1. Step 1: Identify correct option names for ConfigModule

        The correct option to specify a custom env file is envFilePath, and to make it global use isGlobal: true inside forRoot().
      2. Step 2: Eliminate incorrect options

        envFileName and global are invalid option names; forFeature and register are not used for global config loading.
      3. Final Answer:

        ConfigModule.forRoot({ envFilePath: 'custom.env', isGlobal: true }) -> Option B
      4. Quick Check:

        Custom env + global = envFilePath + isGlobal true [OK]
      Hint: Use envFilePath and isGlobal in forRoot for custom global config [OK]
      Common Mistakes:
      • Using envFileName instead of envFilePath
      • Trying to use forFeature for global config
      • Using register instead of forRoot
      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