Free 40-page Claude guide — setup, 120 prompt codes, MCP servers, AI agents. Download free →
CLSkills
Documentationbeginner

Env Example

Share

Generate .env.example from existing .env files

Works with OpenClaude

You 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 .env file exists in your project root: ls -la .env
  • Check if .env.example already exists: test -f .env.example && echo "exists" || echo "not found"
  • Confirm Node.js is available: node --version

Steps

  1. Read the .env file using Node.js fs.readFileSync() with UTF-8 encoding
  2. Split the file contents by newlines to process each line individually
  3. Filter and process each line—skip blank lines and lines starting with # (comments)
  4. For lines with KEY=VALUE format, use regex to extract the key part: /^([^=]+)=/
  5. Replace the value with a placeholder like your_value_here or empty string
  6. Preserve lines that are comments or empty for documentation clarity
  7. Join all processed lines back with newline characters
  8. Write the output to .env.example using fs.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

Quick Info

Difficultybeginner
Version1.0.0
AuthorClaude Skills Hub
documentationenvexample

Install command:

curl -o ~/.claude/skills/env-example.md https://claude-skills-hub.vercel.app/skills/documentation/env-example.md

Related Documentation Skills

Other Claude Code skills in the same category — free to download.

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.