0
0
NestJSframework~10 mins

Environment variables in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Environment variables
Start Application
Load .env file
Parse variables
Set process.env variables
Access variables in code
Use variables for config
Application runs with config
The app starts by loading a .env file, parsing its variables, setting them in process.env, then accessing them in code for configuration.
Execution Sample
NestJS
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';

@Module({
  imports: [ConfigModule.forRoot()],
})
export class AppModule {}
This code loads environment variables from a .env file into process.env when the app starts.
Execution Table
StepActionprocess.env stateResult
1Application starts{}No env vars loaded yet
2ConfigModule.forRoot() loads .env{"PORT":"3000","DB_HOST":"localhost"}Env vars parsed and set
3Access process.env.PORT{"PORT":"3000","DB_HOST":"localhost"}Returns '3000'
4Access process.env.DB_HOST{"PORT":"3000","DB_HOST":"localhost"}Returns 'localhost'
5Use env vars in config{"PORT":"3000","DB_HOST":"localhost"}App configures server and DB connection
6Application runs{"PORT":"3000","DB_HOST":"localhost"}App uses env vars for behavior
💡 All environment variables loaded and used; app runs with config from .env
Variable Tracker
VariableStartAfter Step 2After Step 6
process.env{}{"PORT":"3000","DB_HOST":"localhost"}{"PORT":"3000","DB_HOST":"localhost"}
Key Moments - 3 Insights
Why do we not see environment variables before ConfigModule.forRoot() runs?
Because process.env is empty initially (see Step 1 in execution_table). The ConfigModule loads and sets variables at Step 2.
How do we access environment variables in NestJS code?
We use process.env.VARIABLE_NAME after ConfigModule.forRoot() has loaded them, as shown in Steps 3 and 4.
What happens if a variable is missing in the .env file?
It won't be set in process.env, so accessing it returns undefined. You should provide defaults or validation.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of process.env after Step 2?
A{"PORT":"3000","DB_HOST":"localhost"}
B{}
C{"PORT":"undefined","DB_HOST":"localhost"}
Dnull
💡 Hint
Check the 'process.env state' column at Step 2 in the execution_table.
At which step does the application start using environment variables for configuration?
AStep 1
BStep 3
CStep 5
DStep 6
💡 Hint
Look for when the app uses env vars for config in the 'Result' column.
If the .env file did not have DB_HOST, what would process.env.DB_HOST be after Step 2?
A"localhost"
Bundefined
C"" (empty string)
Dnull
💡 Hint
Variables not in .env are not set in process.env, see key_moments about missing variables.
Concept Snapshot
NestJS loads environment variables from a .env file using ConfigModule.forRoot().
These variables become available in process.env.
Access them in your code via process.env.VARIABLE_NAME.
Missing variables return undefined.
Use env vars to configure app behavior without hardcoding.
Full Transcript
When a NestJS application starts, it uses ConfigModule.forRoot() to load environment variables from a .env file. This process reads the file, parses key-value pairs, and sets them in the Node.js process.env object. Before loading, process.env is empty for these variables. After loading, variables like PORT and DB_HOST are accessible anywhere in the app via process.env.PORT or process.env.DB_HOST. These values configure the app's behavior, such as which port to listen on or database connection details. If a variable is missing in the .env file, accessing it returns undefined, so defaults or validation are recommended. This approach keeps configuration separate from code, making apps flexible and secure.