.env-
Most modern frameworks naturally understand this hyphenated convention. For example, in (a popular frontend build tool), creating a .env-production file ensures that those specific variables are only loaded when you execute the vite build command.
Most developers immediately add .env to their .gitignore file. They assume anything prefixed with .env is safe. They assume the asterisk covers them:
: Use double quotes if the value contains spaces or special characters. symbol for comments. Best Practices Env variables for browser JavaScript - DEV Community
: It allows the same code to run in different environments (Development, Testing, Production) simply by changing the values in the local file. : Typically follows a format, such as: They assume anything prefixed with
# .github/workflows/security.yml name: Block .env- files on: [push, pull_request] jobs: check-env-files: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Ban .env- pattern run: | if find . -type f -name ".env-*" | grep -q .; then echo "::error::Found .env- files. Rename them immediately." exit 1 fi
Stop using .env files in production entirely. Use your hosting platform's native environment variable manager (AWS Secrets Manager, Heroku Config Vars, GitHub Secrets, Vercel Environment Variables). For local development, use a single .env that never leaves your machine.
A common anti‑pattern is writing conditional logic like: Best Practices Env variables for browser JavaScript -
Using .env files offers several benefits, including:
While this is more secure, the .env file remains the king of local development. It is quick, dirty, and universal.
However, this creates a secondary problem: when a new developer clones the repository, they have no idea what environment variables the application needs to run. const dotenv = require('dotenv')
const dotenv = require('dotenv'); const path = require('path'); // Determine the environment, default to 'local' const environment = process.env.NODE_ENV || 'local'; // Load the specific .env- file dotenv.config( path: path.resolve(__dirname, `.env-$environment`) ); // Access your variables console.log(`Server running on port: $process.env.PORT`); Use code with caution. Production Deployment Strategy
One of the most satisfying aspects of the .env file is how it handles different environments.
Enabling or disabling specific bits of code. Server Settings: Port numbers and logging levels. Understanding .env- Variations
Be careful using ARG or ENV in Dockerfiles for secrets, as they can leave traces in the image history.