Bird
Raised Fist0
NestJSframework~20 mins

Custom configuration files in NestJS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
NestJS Config Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output of this NestJS config service usage?
Given a custom configuration file that exports a function returning an object, what will be the value of configService.get('database.host') after the module loads?
NestJS
import { ConfigModule, ConfigService } from '@nestjs/config';

// config/database.config.ts
export default () => ({
  database: {
    host: 'localhost',
    port: 5432
  }
});

// app.module.ts
import { Module } from '@nestjs/common';
@Module({
  imports: [ConfigModule.forRoot({ load: [require('./config/database.config').default] })],
})
export class AppModule {}

// some.service.ts
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';

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

  getDbHost() {
    return this.configService.get('database.host');
  }
}
A'localhost'
Bundefined
C'database.host'
Dnull
Attempts:
2 left
💡 Hint
Remember that the config loader function returns an object with nested keys.
📝 Syntax
intermediate
2:00remaining
Which option correctly imports and uses a custom config file in NestJS?
You want to load a custom config file named app.config.ts that exports a default function returning an object. Which import and usage in ConfigModule.forRoot is correct?
AConfigModule.forRoot({ load: [import('./config/app.config')] })
BConfigModule.forRoot({ load: [require('./config/app.config').default] })
CConfigModule.forRoot({ load: ['./config/app.config'] })
DConfigModule.forRoot({ load: [() => import('./config/app.config')] })
Attempts:
2 left
💡 Hint
The load option expects an array of functions returning config objects.
🔧 Debug
advanced
2:00remaining
Why does this custom config file cause a runtime error in NestJS?
Consider this custom config file settings.config.ts:
export default () => {
  return {
    apiKey: process.env.API_KEY,
    timeout: parseInt(process.env.TIMEOUT)
  }
}
When running the app, it crashes with TypeError: Cannot read property 'API_KEY' of undefined. What is the cause?
AThe config function must return a Promise but returns an object
BparseInt is used incorrectly causing a runtime error
Cprocess.env is undefined because environment variables are not loaded before config runs
DThe export default syntax is invalid in TypeScript
Attempts:
2 left
💡 Hint
Check if environment variables are available when the config function runs.
❓ state_output
advanced
2:00remaining
What is the value of configService.get('feature.enabled') after this config loads?
Given this custom config file feature.config.ts:
export default () => ({
  feature: {
    enabled: process.env.FEATURE_ENABLED === 'true'
  }
});
And the environment variable FEATURE_ENABLED is not set, what does configService.get('feature.enabled') return?
Afalse
Bundefined
Ctrue
Dnull
Attempts:
2 left
💡 Hint
What is the result of comparing undefined to 'true' with ===?
🧠 Conceptual
expert
2:00remaining
Which statement about NestJS custom configuration files is TRUE?
Select the correct statement about how custom configuration files work in NestJS ConfigModule.
ACustom config files must be JSON files; ConfigModule cannot load TypeScript files directly.
BCustom config files can export any value type; ConfigModule uses only the first loaded config file.
CConfigModule requires custom config files to export classes with static methods for config values.
DCustom config files must export a function returning a plain object; ConfigModule merges all loaded configs into one object.
Attempts:
2 left
💡 Hint
Think about how multiple config files combine in NestJS.

Practice

(1/5)
1. What is the main purpose of using custom configuration files in a NestJS application?
easy
A. To write all logic inside configuration files
B. To keep app settings organized and separate from the main code
C. To replace the main app module
D. To store user data permanently

Solution

  1. Step 1: Understand configuration role

    Custom configuration files are used to keep settings like API keys or database info separate from the main code.
  2. Step 2: Identify the correct purpose

    This separation helps organize the app and makes it easier to change settings without touching code logic.
  3. Final Answer:

    To keep app settings organized and separate from the main code -> Option B
  4. Quick Check:

    Custom config files organize settings [OK]
Hint: Custom configs separate settings from code for clarity [OK]
Common Mistakes:
  • Thinking config files hold app logic
  • Confusing config files with database storage
  • Assuming config files replace main modules
2. Which syntax correctly creates a named custom configuration file using registerAs in NestJS?
easy
A. export const config = registerAs('app', () => ({ port: 3000 }));
B. registerAs('app', { port: 3000 });
C. export default registerAs('app', () => ({ port: 3000 }));
D. registerAs('app', () => port: 3000);

Solution

  1. Step 1: Understand registerAs usage

    The registerAs function takes a name and a function returning a config object.
  2. Step 2: Check syntax correctness

    export const config = registerAs('app', () => ({ port: 3000 })); correctly exports a constant assigned to registerAs with a function returning an object.
  3. Final Answer:

    export const config = registerAs('app', () => ({ port: 3000 })); -> Option A
  4. Quick Check:

    registerAs needs a name and a function returning an object [OK]
Hint: Use export const with registerAs and a function returning an object [OK]
Common Mistakes:
  • Omitting export or const keywords
  • Passing object directly instead of a function
  • Incorrect arrow function syntax
3. Given this custom config file:
export const databaseConfig = registerAs('database', () => ({ host: 'localhost', port: 5432 }));

And this module setup:
ConfigModule.forRoot({ load: [databaseConfig] })

What will configService.get('database.host') return?
medium
A. Error: Config not found
B. 'database.host'
C. undefined
D. 'localhost'

Solution

  1. Step 1: Understand config loading

    The databaseConfig is loaded into ConfigModule with load, making its keys accessible.
  2. Step 2: Access nested config value

    Using configService.get('database.host') accesses the 'host' key inside the 'database' config, which is 'localhost'.
  3. Final Answer:

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

    Nested config keys return their values [OK]
Hint: Use dot notation to get nested config values [OK]
Common Mistakes:
  • Expecting the whole object instead of a value
  • Using wrong key names
  • Not loading config before accessing
4. What is wrong with this custom config file code?
export const appConfig = registerAs('app', () => { port: 3000 });
medium
A. The config name 'app' is invalid
B. Missing import of registerAs
C. The arrow function does not return the object properly
D. The port value must be a string

Solution

  1. Step 1: Check arrow function return

    The arrow function uses curly braces without a return statement, so it returns undefined instead of the object.
  2. Step 2: Correct syntax for returning object

    To return an object directly, wrap it in parentheses: () => ({ port: 3000 }).
  3. Final Answer:

    The arrow function does not return the object properly -> Option C
  4. Quick Check:

    Arrow functions need parentheses to return objects directly [OK]
Hint: Wrap object in parentheses to return from arrow function [OK]
Common Mistakes:
  • Using curly braces without return in arrow functions
  • Assuming config name must be special
  • Thinking port must be string
5. You want to create two custom config files, appConfig and dbConfig, and load both in your NestJS app. Which setup correctly loads and accesses the dbConfig.username value?
hard
A. Load both configs in ConfigModule.forRoot({ load: [appConfig, dbConfig] }) and get with configService.get('dbConfig.username')
B. Load only appConfig and get dbConfig.username directly
C. Load both configs but access dbConfig.username with configService.get('username')
D. Load configs separately in different modules without forRoot

Solution

  1. Step 1: Load multiple configs together

    Use ConfigModule.forRoot({ load: [appConfig, dbConfig] }) to load both custom configs at app start.
  2. Step 2: Access nested config by namespace

    Access dbConfig.username using configService.get('dbConfig.username') because configs are namespaced by their registration name.
  3. Final Answer:

    Load both configs in ConfigModule.forRoot({ load: [appConfig, dbConfig] }) and get with configService.get('dbConfig.username') -> Option A
  4. Quick Check:

    Load all configs and use namespaced keys to access [OK]
Hint: Load all configs in forRoot and use dot keys to access [OK]
Common Mistakes:
  • Loading only one config but accessing another
  • Accessing keys without namespace prefix
  • Trying to load configs outside 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