Generate .env.example from existing .env files
✓Works with OpenClaudeYou are a Node.js developer automating documentation tasks. The user wants to generate a .env.example file from an existing .env file by stripping out sensitive values while preserving variable names and structure.
What to check first
- Verify the
.envfile exists in your project root:ls -la .env - Check if
.env.examplealready exists:test -f .env.example && echo "exists" || echo "not found" - Confirm Node.js is available:
node --version
Steps
- Read the
.envfile using Node.jsfs.readFileSync()with UTF-8 encoding - Split the file contents by newlines to process each line individually
- Filter and process each line—skip blank lines and lines starting with
#(comments) - For lines with
KEY=VALUEformat, use regex to extract the key part:/^([^=]+)=/ - Replace the value with a placeholder like
your_value_hereor empty string - Preserve lines that are comments or empty for documentation clarity
- Join all processed lines back with newline characters
- Write the output to
.env.exampleusingfs.writeFileSync()
Code
const fs = require('fs');
const path = require('path');
function generateEnvExample() {
const envPath = path.join(process.cwd(), '.env');
const examplePath = path.join(process.cwd(), '.env.example');
// Check if .env file exists
if (!fs.existsSync(envPath)) {
console.error('Error: .env file not found');
process.exit(1);
}
try {
// Read the .env file
const envContent = fs.readFileSync(envPath, 'utf-8');
const lines = envContent.split('\n');
// Process each line
const exampleLines = lines.map((line) => {
// Preserve empty lines
if (line.trim() === '') {
return '';
}
// Preserve comment lines
if (line.trim().startsWith('#')) {
return line;
}
// Extract key and replace value
const keyMatch = line.match(/^([^=]+)=/);
if (keyMatch) {
const key = keyMatch[1];
return `${key}=`;
}
// Return line as-is if no match
return line;
});
// Write the example file
const exampleContent = exampleLines.join('\n');
fs.writeFileSync(examplePath, exampleContent, 'utf-8');
console.log(`✓ Generated .env.example from .env`);
console.log(`✓ Written to: ${examplePath}`);
} catch (error) {
console.error('Error generating .env.example:', error.message);
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Documentation Skills
Other Claude Code skills in the same category — free to download.
README Generator
Generate comprehensive README.md
API Docs Generator
Generate API documentation
JSDoc Generator
Add JSDoc comments to functions and classes
Architecture Doc
Generate architecture documentation with diagrams
Contributing Guide
Create CONTRIBUTING.md with guidelines
Code of Conduct
Generate CODE_OF_CONDUCT.md
License Picker
Choose and add appropriate LICENSE file
Migration Guide
Create migration/upgrade guide for breaking changes
Want a Documentation skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.