QA
Beginner LevelNode JS

How do you manage environment variables in Node.js (process.env and native --env-file)?

❓ Interview Prompt: Explain process.env, loading .env files, NODE_ENV conventions (development vs production), and native Node 20+ --env-file flag.

Environment configuration keeps secrets and settings out of source code:

•
'process.env': Exposes environment variables passed to the Node process.
•
'NODE_ENV': Standard convention differentiating 'development', 'staging', and 'production' modes.
•
Native Node 20+ '--env-file=.env': Loads '.env' file variables into 'process.env' natively without 'dotenv' packages!

Code Implementation Example

javascript
// Execute via: node --env-file=.env server.js

const port = process.env.PORT || 3000;
const dbURI = process.env.DATABASE_URI;
const isProd = process.env.NODE_ENV === 'production';

console.log('Server starting on port ${port} in ${process.env.NODE_ENV} mode.');
💡Reading environment variables using native Node 20+ --env-file support.
🎯 Interview Key Takeaway

Use process.env for configuration, and native --env-file=.env in Node 20+ to eliminate external dotenv dependencies.

#process.env#environment variables#--env-file#Beginner
Back to all Node JS Questions→

More Node JS Interview Questions

Beginner LevelNode JS

How does the Node.js Event Loop work? Explain the 6 execution phases (Timers, Pending, Poll, Check, Close).

Beginner LevelNode JS

What does 'Single-Threaded, Event-Driven, Non-Blocking I/O' mean in Node.js?

Beginner LevelNode JS

What is the difference between process.nextTick() and setImmediate()?