0
0
NestJSframework~20 mins

Environment variables in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Environment Variables Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
How does NestJS load environment variables?
Consider a NestJS application using the ConfigModule. What is the behavior when you import ConfigModule.forRoot() without any options?
NestJS
import { ConfigModule } from '@nestjs/config';

@Module({
  imports: [ConfigModule.forRoot()],
})
export class AppModule {}
AIt requires you to specify the path to the .env file explicitly or it won't load any variables.
BIt only loads environment variables from process.env and ignores any .env files.
CIt loads environment variables from a .env file in the project root by default.
DIt throws an error if no .env file is found in the project root.
Attempts:
2 left
💡 Hint
Think about the default behavior of ConfigModule.forRoot() in NestJS.
state_output
intermediate
2:00remaining
What value does ConfigService return?
Given the following .env file and NestJS service code, what will be the output of the console.log?
NestJS
.env file content:
PORT=3000

Service code:
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';

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

  printPort() {
    console.log(this.configService.get('PORT'));
  }
}
A3000
Bundefined
Cnull
DThrows an error because PORT is a number but get returns string
Attempts:
2 left
💡 Hint
Remember how ConfigService reads values from .env files.
📝 Syntax
advanced
2:00remaining
Which option correctly loads a custom .env file path?
You want to load environment variables from a file named '.env.production' instead of the default '.env'. Which ConfigModule import is correct?
AConfigModule.forRoot({ envFile: '.env.production' })
BConfigModule.forRoot({ path: '.env.production' })
CConfigModule.forRoot({ env_path: '.env.production' })
DConfigModule.forRoot({ envFilePath: '.env.production' })
Attempts:
2 left
💡 Hint
Check the exact option name for specifying the env file path in ConfigModule.
🔧 Debug
advanced
2:00remaining
Why does ConfigService.get() return undefined?
You have a .env file with API_KEY=abc123 but calling configService.get('API_KEY') returns undefined. What is the most likely cause?
NestJS
import { ConfigModule, ConfigService } from '@nestjs/config';

@Module({
  imports: [ConfigModule.forRoot({ isGlobal: true })],
})
export class AppModule {}

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

  getKey() {
    return this.configService.get('API_KEY');
  }
}
AYou must call configService.load() before using get().
BThe .env file is not in the project root or not loaded properly.
CConfigService.get() only works for variables starting with NEST_ prefix.
DAPI_KEY is a reserved keyword and cannot be accessed.
Attempts:
2 left
💡 Hint
Check the location and loading of the .env file.
🧠 Conceptual
expert
2:00remaining
What is the effect of setting isGlobal: true in ConfigModule.forRoot()?
In a large NestJS app, you use ConfigModule.forRoot({ isGlobal: true }). What does this change in how you use ConfigModule?
AConfigModule is automatically available in all modules without needing to import it again.
BConfigModule variables become global environment variables accessible outside the app.
CConfigModule disables caching of environment variables for every request.
DConfigModule requires manual import in every module despite isGlobal: true.
Attempts:
2 left
💡 Hint
Think about module imports and global scope in NestJS.