A common security mistake in software development is placing credentials and environment-specific settings directly in source code. Database passwords, API keys, and authentication tokens can then be copied into Git repositories, shared with more people than intended, or accidentally exposed when code is published. Environment variables provide a cleaner way to separate configuration from application logic, and .env files are a widely used convenience for local development.
What Is a .env File?
A .env file is a plain-text file containing key-value pairs that describe configuration values for an application. A library such as dotenv can read the file and load those values into the application’s process environment. The application can then access them without hard-coding the secret or setting into the source file.
A simple example might look like this:
DB_HOST=localhost
DB_PORT=5432
DB_USER=app_user
DB_PASSWORD=replace-with-a-secret
API_KEY=replace-with-an-api-key
APP_ENV=developmentThe values above are placeholders. Never copy real production credentials into a tutorial, public repository, or example file.
Load Configuration in Common Runtimes
Node.js
The dotenv package is commonly used in Node.js projects. After installing it, load the file near the application’s entry point:
require('dotenv').config();
const dbPassword = process.env.DB_PASSWORD;Python
Python projects often use python-dotenv to load values before reading them through the os module:
from dotenv import load_dotenv
import os
load_dotenv()
api_key = os.getenv("API_KEY")Some frameworks load environment configuration automatically. Check the framework’s current documentation so that values are loaded once, in the correct order, and only in the intended environment.
Common Security Risks
A .env file is not a security boundary by itself. The biggest mistake is committing it to version control. Another risk occurs when the project directory is exposed through a web server and the server allows direct requests for hidden configuration files. If a file containing credentials becomes public, assume the secrets may have been copied even after the file is removed.
Safer Configuration Practices
- Use a template. Commit a
.env.examplefile containing required variable names and safe placeholder values, never real secrets. - Ignore local secrets. Add
.envand other secret-bearing files to.gitignorebefore the first commit. - Protect web access. Keep configuration files outside the public document root where possible, and explicitly deny access to hidden files at the web-server layer.
- Use a secret manager in production. Inject secrets through the hosting platform, container orchestrator, or dedicated secret-management service rather than relying on a manually copied file.
- Rotate exposed credentials. If a secret may have been committed or shared, revoke and replace it. Removing the file from the latest commit is not enough.
Structuring Configuration Across Environments
Most applications run in more than one environment: a developer machine, a staging server, and production. Each environment needs different values for the same settings, such as database hosts, API endpoints, and feature flags. Keeping a separate .env file per environment is a common starting point, but it only works when the files are clearly named and never mixed up. A file meant for development that is accidentally loaded in production can point the live system at a test database.
A practical habit is to separate settings into two groups: values that are safe to share, such as feature flags and public endpoints, and values that must stay secret, such as passwords and private keys. The safe values can live in version control or in a shared configuration file, while the secrets are injected by the deployment platform. This split reduces the number of secrets that need manual handling and makes it easier to review what changes between environments.
Document the full list of required variables in one place, such as a .env.example file or a short section in the project readme. For each variable, note what it controls, what a sensible default is, and whether it is required or optional. New team members can then set up a working environment without guessing, and missing variables are caught early instead of causing failures at runtime.
Detecting Leaked Secrets Before They Spread
Adding .env to .gitignore protects future commits, but it does nothing about secrets that were already committed. History scanning tools can search a repository for patterns that look like API keys, tokens, or passwords, and many teams run such scans in continuous integration so that a mistake is caught before it is merged. Running a scan once is not enough; new patterns and new secret formats appear over time, so the scan configuration needs occasional review.
Pre-commit hooks offer an earlier line of defence. A hook that runs before each commit can warn the developer while the change is still on their own machine, where fixing it costs seconds rather than hours. Hooks are not foolproof, because they can be skipped or bypassed, so they should complement server-side checks rather than replace them.
If a scan finds a secret, act as if it has already been seen. Revoke the credential and issue a replacement, then check logs for any unusual use of the old credential. Simply deleting the file from the latest commit leaves the secret visible in the repository history, and rewriting history is a disruptive step that should be planned carefully rather than done in a panic.
Handling Secrets in Teams
When several people share a project, secrets need a clear process for onboarding and offboarding. New developers should receive the values they need through a secure channel, such as a secret manager or an encrypted share, never through chat messages or email that remain searchable afterwards. A short checklist for onboarding prevents the common situation where a new team member spends a day chasing missing configuration.
When someone leaves the team, review which secrets they could access and rotate the most sensitive ones. This is not a question of trust; it is basic hygiene, the same as returning office keys. The review is much easier when access is granted through named accounts and roles rather than through a single shared credential that everyone uses.
Rotation deserves a schedule of its own. Secrets that never change become more valuable to an attacker the longer they exist, and rotating them under pressure after an incident is far harder than rotating them on a calm, planned timetable. Start with the highest-value credentials, automate the rotation where the platform supports it, and keep a record of when each secret was last changed.
Conclusion
Separating configuration from code improves portability and reduces accidental exposure, but it only works when secrets are handled carefully. Use templates, ignore local files, protect the web root, and prefer managed secrets in production. For more guidance, read the Twelve-Factor App configuration principles and our guide to managing API keys securely.

Leave a comment