Environment variables are external values used to configure applications without changing source code.
They are commonly used to store sensitive information and environment-specific settings.
Keep secrets out of source code
Support different environments (development, testing, production)
Improve security
Simplify configuration management
Examples:
Database URLs
API keys
Port numbers
Node.js provides the process.env object.
.env Files (dotenv Package)Managing many environment variables manually is difficult.
The dotenv package loads variables from a .env file.
.env File
Never commit .env files to Git
Add .env to .gitignore
Use strong secret values
Rotate secrets regularly
| Variable | Purpose |
|---|---|
PORT | Server port |
NODE_ENV | App environment |
DB_URL | Database connection |
API_KEY | External service key |
❌ Hardcoding secrets
❌ Forgetting to load dotenv
❌ Using wrong variable names
Environment variables store configuration data
Accessed via process.env
dotenv simplifies management
Essential for secure Node.js applications
Take quizzes related to this topic and see where you stand!
Start Quiz Now