0
0
NestJSframework~20 mins

Environment-based configuration in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NestJS Env Config Master
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 with ConfigModule?

Consider this NestJS module setup:

import { ConfigModule } from '@nestjs/config';

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

What happens when the app starts?

ANestJS requires manual import of ConfigService in every module to access variables.
BNestJS ignores .env files and only uses system environment variables.
CNestJS reads variables from a .env file and makes them available globally via ConfigService.
DNestJS throws an error if .env file is missing, stopping the app.
Attempts:
2 left
💡 Hint

Think about what isGlobal: true means in ConfigModule.

📝 Syntax
intermediate
2:00remaining
Which code correctly accesses an environment variable in a NestJS service?

Given the ConfigService is injected, which code snippet correctly gets the DATABASE_URL variable?

NestJS
constructor(private configService: ConfigService) {}

getDbUrl() {
  // Which line is correct?
}
Areturn this.configService.getDatabaseUrl();
Breturn process.env.DATABASE_URL();
Creturn this.configService.env.DATABASE_URL;
Dreturn this.configService.get('DATABASE_URL');
Attempts:
2 left
💡 Hint

Check the official method to get env variables from ConfigService.

🔧 Debug
advanced
2:00remaining
Why does this NestJS app fail to load environment variables?

Look at this code snippet:

ConfigModule.forRoot({
  envFilePath: '.env.production',
  isGlobal: true,
});

The app does not load variables from .env.production. What is the likely cause?

AThe <code>isGlobal</code> option disables loading environment files.
BThe <code>envFilePath</code> path is relative to the process working directory, which may differ from the source file location.
CThe <code>ConfigModule</code> must be imported in every module to load env files.
DNestJS only supports <code>.env</code> files and ignores custom filenames.
Attempts:
2 left
💡 Hint

Consider where the app runs and where the file is located.

state_output
advanced
2:00remaining
What is the value of config after this code runs?

Given this code:

ConfigModule.forRoot({
  load: [() => ({
    port: parseInt(process.env.PORT) || 3000,
    mode: process.env.MODE || 'dev',
  })],
  isGlobal: true,
});

// Assume process.env.PORT = '0', process.env.MODE is undefined

const config = configService.get('port');

What is the value of config?

A0
BNaN
C3000
Dundefined
Attempts:
2 left
💡 Hint

Remember how parseInt and || work with 0.

🧠 Conceptual
expert
3:00remaining
Which statement about NestJS environment configuration is TRUE?

Choose the correct statement about environment-based configuration in NestJS.

AConfigModule can load multiple environment files conditionally based on NODE_ENV using a custom function in envFilePath.
BConfigModule requires manual injection in every service to access environment variables.
CEnvironment variables must be declared in a TypeScript interface to be accessible via ConfigService.
DConfigService automatically reloads environment variables if the .env file changes during runtime.
Attempts:
2 left
💡 Hint

Think about how to load different env files for development and production.