Challenge - 5 Problems
Config Mastery in NestJS
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
Why use configuration management in NestJS?
Which of the following best explains why configuration management is important in a NestJS application?
Attempts:
2 left
💡 Hint
Think about how apps behave differently in development, testing, and production.
✗ Incorrect
Configuration management lets you keep environment-specific settings separate from your code. This helps you run the same app in different places without changing the code itself.
❓ component_behavior
intermediate2:00remaining
Behavior of ConfigService in NestJS
What will be the output of the following code snippet when the environment variable PORT is set to 3000?
NestJS
import { ConfigService } from '@nestjs/config'; const configService = new ConfigService(); const port = configService.get('PORT'); console.log(port);
Attempts:
2 left
💡 Hint
ConfigService reads environment variables by default.
✗ Incorrect
ConfigService reads environment variables like PORT and returns their values. However, ConfigService should be injected by NestJS's dependency injection system. Instantiating it directly without injection will not load environment variables, so it will not return '3000'.
📝 Syntax
advanced2:00remaining
Correct way to load configuration in NestJS
Which option correctly shows how to load a configuration file using ConfigModule in a NestJS application?
NestJS
import { ConfigModule } from '@nestjs/config'; @Module({ imports: [ // Which option is correct here? ], }) export class AppModule {}
Attempts:
2 left
💡 Hint
Check the official method to load environment files in ConfigModule.
✗ Incorrect
ConfigModule.forRoot accepts an option 'envFilePath' to specify the environment file to load. Other methods do not exist or are incorrect.
🔧 Debug
advanced2:00remaining
Why does ConfigService return undefined?
Given this NestJS service code, why does configService.get('DATABASE_URL') return undefined?
@Module({
imports: [ConfigModule.forRoot()],
})
export class AppModule {}
@Injectable()
export class DatabaseService {
constructor(private configService: ConfigService) {}
getDatabaseUrl() {
return this.configService.get('DATABASE_URL');
}
}
Attempts:
2 left
💡 Hint
Check if the environment variable exists where the app runs.
✗ Incorrect
If DATABASE_URL is not set in the environment or .env file, ConfigService.get returns undefined because it cannot find the value.
❓ lifecycle
expert3:00remaining
When is configuration loaded in NestJS lifecycle?
At what point in the NestJS application lifecycle is the configuration loaded and available via ConfigService?
Attempts:
2 left
💡 Hint
Think about when environment variables must be ready for use.
✗ Incorrect
ConfigModule.forRoot loads configuration during the bootstrap phase, making ConfigService values available to all modules and services during initialization.