Lesson 28 of 30
Environment Variables
93% complete
Chapters
Chapter 28
Environment Variables
Learn how environment variables are used to configure Node.js applications.
Theory
Environment variables allow applications to read configuration values from the environment.
They are commonly used for:
• Database connection strings
• API keys
• Port numbers
• Application configuration
• Secrets
In Node.js, environment variables are available through process.env.
Sensitive values should not be hard-coded directly into source code or committed to public repositories.
Example
Node.js Example
const port = process.env.PORT || 3000;
console.log("Server port:", port);Output
The application uses the PORT environment variable when available.
Practice
Create a .env file and load a development configuration value using dotenv.
Interview Questions
1.
What are environment variables?
2.
What is process.env?
3.
Why should secrets not be hard-coded?
Quick Quiz
Which option best matches this chapter?
A.Configuration values provided by the environment
B.HTML elements
C.Database rows
D.CSS classes
Answer: Configuration values provided by the environment