# Stop Committing Secrets: Automatically Generate .env.example with gen-envex

When working on Node.js, React, Next.js, or any modern application, one of the first things you'll create is a `.env` file.

It stores everything from database credentials to API keys and authentication secrets.

```env
DATABASE_URL=postgres://...
JWT_SECRET=super-secret-key
STRIPE_SECRET_KEY=sk_live_xxxxxxxxx
NEXT_PUBLIC_API_URL=https://api.example.com
```

The problem?

You should **never** commit this file to Git.

But your teammates still need to know which environment variables are required to run the project.

That's why almost every project includes a `.env.example`.

# The Problem

Keeping `.env.example` updated is surprisingly annoying.

Imagine adding a new environment variable.

```env
REDIS_URL=redis://localhost:6379
```

Now you also need to remember to update:

```env
REDIS_URL=
```

inside `.env.example`.

Most developers forget.

Eventually your repository looks like this:

```plaintext
❌ Missing variables
❌ Outdated documentation
❌ New contributors can't run the project
```

# The Solution

I built **gen-envex** to solve exactly this problem.

Instead of maintaining `.env.example` manually, it generates one automatically from your existing `.env`.

Your secrets are removed, while the file structure stays exactly the same.

Input:

```env
# Database
DB_HOST=localhost
DB_PORT=5432
DB_PASSWORD=mysecretpassword

# API
NEXT_PUBLIC_API_URL=https://api.example.com
STRIPE_SECRET_KEY=sk_live_xxxxx
```

Generated output:

```env
# Database
DB_HOST=
DB_PORT=
DB_PASSWORD=

# API
NEXT_PUBLIC_API_URL=
STRIPE_SECRET_KEY=
```

Simple.

Safe.

Automatic.

# Features

✅ Removes all secret values

✅ Preserves comments

✅ Preserves blank lines

✅ Maintains variable order

✅ Supports custom input/output files

✅ Works on Windows, macOS, and Linux

# Installation

Install globally:

```bash
npm install -g gen-envex
```

Or use it without installing:

```bash
npx gen-envex
```

Or install it as a development dependency:

```bash
npm install --save-dev gen-envex
```

# Basic Usage

Run:

```bash
gen-envex
```

or

```bash
npx gen-envex
```

This generates:

```plaintext
.env.example
```

from

```plaintext
.env
```

# Custom Files

Need different file names?

```bash
gen-envex --input .env.local --output .env.example
```

or

```bash
gen-envex --input config/.env --output config/.env.example
```

# Automate Everything

The best part is that you never have to think about it again.

Add it to your `package.json`.

```json
{
  "scripts": {
    "dev": "gen-envex && next dev"
  }
}
```

Now every time you start your development server,

```bash
npm run dev
```

your `.env.example` is automatically updated.

No extra commands.

No forgotten variables.

No outdated examples.

# Why This Matters

Open-source projects often receive issues like:

> "The app doesn't start."

or

> "Which environment variables do I need?"

In many cases, the answer is simply that `.env.example` wasn't updated.

Keeping it synchronized makes onboarding dramatically easier for teammates and contributors.

It's a tiny improvement that saves time for everyone.

# Standalone Script

Don't want to install a package?

`gen-envex` also provides a standalone script.

Simply copy `generate-env-example.js` into your project and run:

```bash
node generate-env-example.js
```

You can even integrate it into your development workflow:

```json
{
  "scripts": {
    "dev": "node generate-env-example.js && next dev"
  }
}
```

# Open Source

If you find the project useful, I'd love your feedback.

⭐ GitHub: [https://github.com/AniketDey06/gen-envex](https://github.com/AniketDey06/gen-envex)

📦 npm: [https://www.npmjs.com/package/gen-envex](https://www.npmjs.com/package/gen-envex)

Issues, feature requests, and pull requests are always welcome.

# Final Thoughts

Sometimes the best developer tools solve small problems that every developer has experienced.

`gen-envex` is one of those tools.

It removes one more thing you have to remember, keeps your repositories safer, and makes collaboration easier.

If you're tired of manually maintaining `.env.example`, give it a try.

Happy coding! 🚀
