.env- ^hot^ -
# Inside GitLab CI vault kv get -field=value secret/production > .env-production
.env-development (or .env-local ): Used by developers on their laptops. It often contains pointers to local databases ( localhost ) and features strict debugging mode turned on .
DATABASE_URL=postgres://prod-user:secret@prod-db:5432/app_prod LOG_LEVEL=info PORT=8080 # Inside GitLab CI vault kv get -field=value
Use the dotenv package. require('dotenv').config() or import 'dotenv/config' . Python: Use python-dotenv . PHP: Use phpdotenv .
Understanding the .env File: The Cornerstone of Modern Environment Management require('dotenv')
Maintain a canonical list of required variables. Use a validation library that throws a descriptive error on missing keys. For example:
.env-test : Used by automated testing frameworks. This file typically points to a temporary "mock" database that can be wiped clean after every test suite run. Understanding the
const env = process.argv[2] || 'development'; const envFile = path.join(__dirname, .env-$env );
As software developers, we often find ourselves juggling multiple projects simultaneously, each with its own set of dependencies, configurations, and environment variables. Managing these variables can become a daunting task, especially when dealing with sensitive information such as API keys, database credentials, or encryption secrets. This is where .env files come into play, providing a simple yet effective solution for managing environment variables across various projects and applications.
# This is a comment inside a .env-development file PORT=3000 DATABASE_URL="postgresql://localhost:5432/my_dev_db" API_KEY="dev_secret_key_abc123" DEBUG=true Use code with caution. Why You Need Environment-Specific Files