Environment Variables & Configuration
Environment Variables & Configuration
Environment variables allow you to separate your application’s code from its configuration. This is essential for managing secrets (like API keys) and changing settings based on the environment (development, staging, production).
1. Why use Environment Variables?
- Security: Prevent hardcoding sensitive credentials in source code.
- Flexibility: Run the same code with different settings across multiple environments.
- Standardization: Most deployment platforms (Heroku, AWS, Docker) use environment variables for configuration.
2. Using dotenv
The dotenv package is the standard way to load environment variables from a .env file into process.env.
Installation
npm install dotenvCreating a .env file
Create a file named .env in the root of your project:
PORT=5000DB_URL=mongodb://localhost:27017/myappAPI_KEY=your_secret_api_keyLoading Variables
import 'dotenv/config';
const port = process.env.PORT || 3000;console.log(`Server will run on port: ${port}`);console.log(`Database URL: ${process.env.DB_URL}`);3. Best Practices
- Never commit
.envfiles: Add.envto your.gitignorefile to prevent secrets from being leaked to version control. - Use
.env.example: Create a template file with dummy values to show other developers what variables are needed. - Validation: Use libraries like
envalidorjoito ensure all required variables are present and correctly formatted at startup.
[!WARNING] Storing secrets in plain text in
.envfiles is only safe if access to the server and version control is restricted. For highly sensitive environments, consider using secret management services like AWS Secrets Manager or HashiCorp Vault.