What is environment-based configuration in NestJS?
It is a way to set different settings for your app depending on where it runs, like development, testing, or production. NestJS uses environment variables to manage these settings.
Click to reveal answer
beginner
How do you load environment variables in a NestJS application?
You use the ConfigModule from '@nestjs/config' and call ConfigModule.forRoot() in your root module. This loads variables from a .env file into your app.
Click to reveal answer
beginner
Why should you avoid hardcoding configuration values in your NestJS app?
Hardcoding makes it hard to change settings for different environments and can expose sensitive info. Using environment variables keeps config flexible and secure.
Click to reveal answer
intermediate
How can you access environment variables inside a NestJS service?
Inject the ConfigService from '@nestjs/config' into your service, then use configService.get('VARIABLE_NAME') to get the value.
Click to reveal answer
intermediate
What is the benefit of using validation with environment variables in NestJS?
Validation ensures required variables exist and have correct formats, preventing runtime errors and making your app more reliable.
Click to reveal answer
Which NestJS module is used to load environment variables?
AHttpModule
BLoggerModule
CDatabaseModule
DConfigModule
✗ Incorrect
ConfigModule is designed to load and manage environment variables in NestJS.
Where do you typically store environment variables for local development?
Apackage.json
B.env file
Cmain.ts
DREADME.md
✗ Incorrect
The .env file is the standard place to store environment variables locally.
How do you access an environment variable named 'PORT' inside a NestJS service?
AconfigService.get('PORT')
BConfigModule.PORT
Cthis.PORT
Dprocess.env.PORT
✗ Incorrect
Using configService.get('PORT') is the recommended way to access env variables in NestJS services.
What is a key advantage of using environment-based configuration?
AHardcoding values
BIgnoring security
CChanging settings without code changes
DIncreasing app size
✗ Incorrect
Environment-based config lets you change settings like database URLs without changing code.
Which method validates environment variables in NestJS?
AUsing Joi schema with ConfigModule
BUsing console.log
CUsing process.exit()
DUsing HttpModule
✗ Incorrect
Joi schema validation can be integrated with ConfigModule to check env variables.
Explain how to set up environment-based configuration in a NestJS app.
Think about loading, storing, and accessing environment variables.
You got /4 concepts.
Describe why environment-based configuration is important for different deployment stages.
Consider how apps behave differently in various environments.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using environment-based configuration in a NestJS application?
easy
A. To write more complex code logic
B. To hardcode all secrets inside the source files
C. To increase the size of the application
D. To change app settings without modifying the code
Hint: ConfigService.get returns env var value [OK]
Common Mistakes:
Expecting the key name instead of its value
Assuming undefined if variable exists
Confusing null with undefined
4. You wrote this code to load environment variables in NestJS:
import { ConfigModule } from '@nestjs/config';
@Module({
imports: [ConfigModule.forRoot()],
})
export class AppModule {}
But your app crashes with an error saying environment variables are undefined. What is the likely cause?
medium
A. You must call ConfigService.get() inside main.ts only
B. You forgot to create a .env file or set environment variables
C. You need to import HttpModule as well
D. ConfigModule.forRoot() should be called inside a service, not module
Solution
Step 1: Check environment variable source
ConfigModule loads variables from .env file or system environment. If missing, variables are undefined.
Step 2: Identify common mistake
Not having a .env file or not setting variables causes undefined values and app crash.
Final Answer:
You forgot to create a .env file or set environment variables -> Option B
Quick Check:
Missing .env file = undefined env vars [OK]
Hint: Always create .env file or set env vars [OK]
Common Mistakes:
Thinking ConfigModule.forRoot() is misplaced
Assuming HttpModule is needed for config
Calling ConfigService.get() only in main.ts
5. You want to load different database URLs for development and production in NestJS using environment variables. Which approach correctly uses ConfigModule to load .env.development or .env.production based on NODE_ENV?
hard
A. ConfigModule.forRoot({ envFilePath: `.env.${process.env.NODE_ENV}` })
B. ConfigModule.forRoot({ envFilePath: '.env' })
C. ConfigModule.forRoot({ envFilePath: '.env.production' }) always
D. ConfigModule.forRoot({ envFilePath: '.env.development' }) always
Solution
Step 1: Understand dynamic env file loading
Using envFilePath with template string allows loading .env files based on NODE_ENV value.
Step 2: Check options
ConfigModule.forRoot({ envFilePath: `.env.${process.env.NODE_ENV}` }) dynamically loads .env.development or .env.production depending on NODE_ENV, others load fixed files.
Final Answer:
ConfigModule.forRoot({ envFilePath: `.env.${process.env.NODE_ENV}` }) -> Option A