📖 Scenario: You are building a NestJS application that needs to manage configuration settings safely and efficiently.
🎯 Goal: Set up the ConfigModule in a NestJS app to load environment variables and make them available throughout the app.
📋 What You'll Learn
Create a configuration file with environment variables
Import ConfigModule in the root module
Configure ConfigModule to load environment variables globally
Use ConfigService to access configuration values
💡 Why This Matters
🌍 Real World
Managing environment variables is essential for configuring apps differently in development, testing, and production without changing code.
💼 Career
Understanding ConfigModule setup is important for backend developers working with NestJS to build scalable and maintainable applications.
Progress0 / 4 steps
1
Create a .env file with environment variables
Create a file named .env in the project root with these exact entries: DATABASE_HOST=localhost DATABASE_PORT=5432 API_KEY=12345
NestJS
Hint
The .env file contains key-value pairs for configuration.
2
Import ConfigModule in AppModule
In app.module.ts, import ConfigModule from @nestjs/config and add ConfigModule.forRoot() to the imports array of @Module.
NestJS
Hint
Use ConfigModule.forRoot() to load environment variables automatically.
3
Configure ConfigModule to be global
Modify the ConfigModule.forRoot() call in app.module.ts to include { isGlobal: true } so the configuration is available throughout the app.
NestJS
Hint
Setting isGlobal: true makes ConfigModule available everywhere without importing again.
4
Use ConfigService to access environment variables
In any service, inject ConfigService from @nestjs/config in the constructor and use this.configService.get('DATABASE_HOST') to access the DATABASE_HOST value.
NestJS
Hint
Inject ConfigService in the constructor and call get() with the environment variable name.
Practice
(1/5)
1. What is the main purpose of ConfigModule in a NestJS application?
easy
A. To manage application settings using environment variables
B. To handle HTTP requests and responses
C. To connect to a database
D. To create middleware for logging
Solution
Step 1: Understand ConfigModule role
The ConfigModule is designed to load and manage configuration settings, often from environment variables.
Step 2: Compare with other options
Options A, B, and D relate to other parts of NestJS like database connections, HTTP handling, and middleware, not configuration management.
Final Answer:
To manage application settings using environment variables -> Option A
Quick Check:
ConfigModule = Manage app settings [OK]
Hint: ConfigModule is for app settings, not HTTP or DB [OK]
Common Mistakes:
Confusing ConfigModule with database modules
Thinking ConfigModule handles HTTP requests
Assuming ConfigModule creates middleware
2. Which is the correct way to import ConfigModule globally in the root module?
easy
A. ConfigModule.register({ global: true })
B. ConfigModule.forFeature({ isGlobal: true })
C. ConfigModule.forRoot({ isGlobal: true })
D. ConfigModule.global()
Solution
Step 1: Recall ConfigModule global setup syntax
To make ConfigModule available app-wide, use forRoot() with isGlobal: true.
Step 2: Check other options for correctness
forFeature() is for scoped configs, register() and global() are not valid methods here.
Final Answer:
ConfigModule.forRoot({ isGlobal: true }) -> Option C
Quick Check:
Global config = forRoot + isGlobal true [OK]
Hint: Use forRoot with isGlobal true for global config [OK]