TL;DR - Quick Fix
Run this in your terminal:
The Problem
As developers, we often struggle with maintaining .env.example
files in our repositories. The common workflow goes like this:
- Create a
.env.local
with actual values - Manually copy it to
.env.example
- Remove all sensitive values
- Forget to update
.env.example
when adding new variables - Repeat this process every time
This manual process is:
- Time-consuming
- Error-prone
- Easy to forget
- Often leads to outdated example files
The Solution
Add this one-liner to your package.json
scripts:
Now you can generate your .env.example
file with a single command:
How It Works
Let's break down the command:
sed 's/=.*/=/'
: Usessed
to replace everything after=
with nothing.env.local
: Reads from your actual env file> .env.example
: Outputs to the example file
For example, this:
Becomes:
Best Practices
-
Add to Your Development Workflow:
-
Git Hooks: Consider adding this to your pre-commit hook to never forget updating the example file.
-
Documentation: Add comments in your
.env.local
file - they'll be preserved in the example:
When to Use This
This command is particularly useful when:
- Starting a new project
- Adding new environment variables
- Sharing your project with other developers
- Maintaining open-source projects
- Ensuring your example stays in sync with actual requirements
Pro Tips
-
Custom Variations:
-
Add Default Values:
Note: This command works on Unix-based systems (macOS, Linux). For Windows, you might need to use WSL or alternative commands.