What are environment variables in the context of a NestJS application?
Environment variables are key-value pairs stored outside the code that hold configuration data. They help keep sensitive info like API keys or database URLs separate from the codebase.
Click to reveal answer
beginner
How do you load environment variables in a NestJS app?
You use the @nestjs/config package which reads variables from a .env file and makes them available via ConfigService throughout your app.
Click to reveal answer
beginner
What is the purpose of the ConfigModule in NestJS?
ConfigModule loads environment variables and provides ConfigService to access them safely anywhere in your app.
Click to reveal answer
beginner
How can you access an environment variable called DATABASE_URL inside a NestJS service?
Inject ConfigService and call configService.get('DATABASE_URL') to get the value of DATABASE_URL.
Click to reveal answer
beginner
Why should you avoid hardcoding sensitive data in your NestJS code?
Hardcoding secrets risks exposing them in code repositories. Environment variables keep secrets outside code, improving security and flexibility.
Click to reveal answer
Which NestJS package helps manage environment variables?
A@nestjs/config
B@nestjs/env
C@nestjs/dotenv
D@nestjs/settings
✗ Incorrect
The @nestjs/config package is the official way to load and manage environment variables in NestJS.
Where do you typically store environment variables for a NestJS app?
AInside the main.ts file
BIn the .env file
CIn the package.json file
DIn the node_modules folder
✗ Incorrect
Environment variables are usually stored in a .env file at the root of the project.
How do you access environment variables inside a NestJS service?
AUsing global variables
BUsing process.env.VARIABLE_NAME directly
CUsing import from dotenv package
DUsing ConfigService.get('VARIABLE_NAME')
✗ Incorrect
The recommended way is to inject ConfigService and use its get method to access variables.
What is a key benefit of using environment variables in NestJS?
ASeparates config from code for security and flexibility
BMakes the app run faster
CAutomatically documents your code
DPrevents all bugs
✗ Incorrect
Environment variables keep sensitive data out of code and allow easy config changes without code edits.
Which method loads environment variables into NestJS at startup?
Aprocess.env.load()
BAppModule.loadEnv()
CConfigModule.forRoot()
DNestFactory.loadEnv()
✗ Incorrect
Calling ConfigModule.forRoot() in your root module loads environment variables from .env.
Explain how to set up and use environment variables in a NestJS project.
Think about the steps from creating the file to reading variables in services.
You got /4 concepts.
Why is it important to use environment variables instead of hardcoding secrets in NestJS?
Consider risks of hardcoding and benefits of separation.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using environment variables in a NestJS application?
easy
A. To hardcode sensitive data like passwords in the source code
B. To write all configuration directly inside the main application file
C. To make the application run faster by avoiding configuration files
D. To keep configuration settings outside the code for security and flexibility
Solution
Step 1: Understand environment variables purpose
Environment variables store settings like API keys or database URLs outside the code to keep them safe and easy to change.
Step 2: Compare options
Only To keep configuration settings outside the code for security and flexibility correctly describes this purpose. Options A and B suggest unsafe practices, and D is incorrect about performance.
Final Answer:
To keep configuration settings outside the code for security and flexibility -> Option D
Quick Check:
Environment variables = external safe config [OK]
Hint: Environment variables keep secrets out of code [OK]
Common Mistakes:
Hardcoding sensitive data inside source code
Confusing environment variables with regular variables
Thinking environment variables improve speed
2. Which of the following is the correct way to load environment variables in a NestJS app?
easy
A. Manually read .env file using fs module in every service
B. Use process.env directly without any module
C. Import ConfigModule with ConfigModule.forRoot() in the root module
D. Declare environment variables inside main.ts without ConfigModule
Solution
Step 1: Identify NestJS recommended method
NestJS uses ConfigModule.forRoot() to load .env files and provide ConfigService.
Step 2: Evaluate other options
Options A and C are possible but not recommended patterns. Declare environment variables inside main.ts without ConfigModule is incorrect because environment variables are external.
Final Answer:
Import ConfigModule with ConfigModule.forRoot() in the root module -> Option C
Quick Check:
ConfigModule.forRoot() loads env [OK]
Hint: Use ConfigModule.forRoot() to load .env [OK]
Common Mistakes:
Reading .env manually everywhere
Ignoring ConfigModule and using process.env directly
Hint: ConfigService.get returns env value by key [OK]
Common Mistakes:
Expecting the key name instead of value
Assuming undefined if ConfigModule not imported
Confusing null with undefined
4. What is wrong with this NestJS module setup for environment variables?
@Module({
imports: [ConfigModule],
})
export class AppModule {}
medium
A. ConfigModule is imported without calling forRoot(), so env variables won't load
B. ConfigModule should be imported in providers, not imports
C. ConfigModule requires a .json config file, not .env
D. ConfigModule must be imported twice to work
Solution
Step 1: Check ConfigModule import syntax
ConfigModule must be imported with forRoot() to load environment variables from .env file.
Step 2: Identify error in code
The code imports ConfigModule directly without forRoot(), so env variables won't be loaded.
Final Answer:
ConfigModule is imported without calling forRoot(), so env variables won't load -> Option A
Quick Check:
ConfigModule.forRoot() needed to load env [OK]
Hint: Always use ConfigModule.forRoot() to load env [OK]
Common Mistakes:
Importing ConfigModule without forRoot()
Placing ConfigModule in providers instead of imports
Thinking ConfigModule needs JSON config
5. You want to provide a default value for an environment variable PORT in NestJS if it is not set in the .env file. Which code snippet correctly achieves this in a service?
hard
A. const port = this.configService.get('PORT') || 3000;
B. const port = process.env.PORT ? 3000 : this.configService.get('PORT');
C. const port = this.configService.get('PORT', 3000);
D. const port = this.configService.get('PORT') && 3000;
Solution
Step 1: Understand default value logic
If PORT is undefined or empty, use 3000 as default. Logical OR (||) works for this.
Step 2: Analyze options
const port = this.configService.get('PORT') || 3000; uses || to provide default 3000 if get('PORT') is falsy. const port = process.env.PORT ? 3000 : this.configService.get('PORT'); reverses logic incorrectly. const port = this.configService.get('PORT', 3000); is invalid because get() does not take default param. const port = this.configService.get('PORT') && 3000; uses && which returns 3000 only if PORT is truthy, wrong logic.
Final Answer:
const port = this.configService.get('PORT') || 3000; -> Option A
Quick Check:
Use || to set default value [OK]
Hint: Use || to set default if env variable missing [OK]
Common Mistakes:
Using && instead of || for default
Passing default value as second argument to get()
Reversing condition logic
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.