genv-ex
v1.1.0
Published
Automatically generates a .env.example file from a .env file with placeholders
Maintainers
Readme
genv-ex
A lightweight Node.js utility to generate a .env.example file from a .env file, replacing sensitive values with placeholders. Ideal for sharing environment variable templates in Node.js and full-stack projects.
📦 Simply (Recommended Usage)
✔️ Install locally
npm install genv-ex✔️ Use in your code
CommonJS (default Node.js)
// index.js (CommonJS)
const genvex = require("genv-ex");
genvex();ES Modules (with "type": "module" in package.json or using .mjs)
// index.js (ESM)
import genvex from "genv-ex";
genvex();Features
- Generates
.env.examplefrom.envwith customizable placeholders. - Preserves comments and empty lines for better readability.
- Supports preserving specific values or ignoring keys.
- Configurable via CLI, programmatic API, or
.genv-exrcfile. - Handles large files efficiently with streaming.
- Interactive CLI prompts for missing
.envfiles. - CommonJS and ESM compatible.
Installation
Install locally in a project:
# install locally (recommended)
npm install genv-exOr globally for CLI use:
npm install -g genv-exFor development with nodemon:
npm install --save-dev nodemonFile Structure
Ensure your project has the following structure:
your-project/
├── .env # Source environment file
├── .genv-exrc # Optional config file
├── node_modules/
├── package.json
└── genv-ex/ # If developing locally
├── index.js # Main module
├── bin/
│ └── cli.js # CLI script
Usage
genv-ex can be used via the command-line interface (CLI) or programmatically in your Node.js code.
CLI Usage
Run genv-ex to generate .env.example from .env with default settings:
npx genv-exThis reads .env, replaces values with <YOUR_VALUE_HERE>, and creates .env.example, preserving comments.
Common Commands
- Specify input/output files:
npx genv-ex -i .env.prod -o .env.prod.example - Preserve specific keys:
npx genv-ex --preserve API_KEY PUBLIC_URL - Ignore keys:
npx genv-ex --ignore SECRET_TOKEN - Exclude comments:
npx genv-ex --no-comments - Preview without writing:
npx genv-ex --dry-run - Create a sample
.env:npx genv-ex --init
Interactive Mode
If no .env file exists, the CLI prompts to create a sample:
$ npx genv-ex
No '.env' file found.
Create a sample .env file? (y/n): y
Created '.env'. Edit it and run again.Help
See all options:
npx genv-ex --helpPackage Scripts
Add to package.json for convenience:
{
"scripts": {
"gen-env": "genv-ex",
"start": "nodemon bin/cli.js"
}
}Run:
npm run gen-envProgrammatic Usage
Use genv-ex in your Node.js code for integration into scripts or applications.
CommonJS
// index.js
const generateEnvExample = require("genv-ex");
generateEnvExample()
.then((result) => console.log("Generated:", result))
.catch((error) => console.error("Error:", error.message));Run:
node index.jsESM
For ESM projects ("type": "module" in package.json):
// index.js
import generateEnvExample from "genv-ex";
async function run() {
const result = await generateEnvExample({
preserveValues: ["API_KEY"],
ignoreKeys: ["SECRET"],
});
console.log("Generated:", result);
}
run();package.json:
{
"type": "module",
"dependencies": {
"genv-ex": "^1.0.0"
}
}Run:
node index.jsExample Output
For a .env file:
# Database config
API_KEY=xyz123
DATABASE_URL=mongodb://localhost:27017
SECRET=hiddenRunning npx genv-ex --preserve API_KEY --ignore SECRET:
# Generated by genv-ex
# Auto-generated on 2025-04-12T12:34:56.789Z
# Database config
API_KEY=xyz123
DATABASE_URL=<YOUR_VALUE_HERE>Console output:
Successfully created '.env.example' with 2 keys
Skipped keys: SECRETConfiguration File
Use a .genv-exrc file to set defaults (supports JSON5 for comments, trailing commas):
{
// Default placeholder
"placeholder": "YOUR_VALUE",
// Preserve these keys
"preserveValues": ["API_KEY", "PUBLIC_URL"],
// Include comments
"includeComments": true
}CLI or programmatic options override .genv-exrc.
Options
| Option | Description | Default |
| ----------------- | ------------------------------------- | --------------------------- |
| envFilePath | Source .env file path | .env |
| outputFilePath | Output .env.example file path | .env.example |
| placeholder | Placeholder for values | <YOUR_VALUE_HERE> |
| preserveValues | Array of keys to keep original values | [] |
| ignoreKeys | Array of keys to exclude | [] |
| includeComments | Preserve comments and empty lines | true |
| header | Custom header text | # Generated by genv-ex... |
| force | Overwrite existing output file | true |
| silent | Suppress console output | false |
| dryRun | Preview output without writing | false |
| configFile | Path to config file | .genv-exrc |
CLI flags (e.g., --no-comments, --no-force) override defaults.
Running with Nodemon
For development, use nodemon to watch for changes:
npm run startTip: Add .env.example to .nodemonignore to prevent infinite loops:
.env.exampleIf you encounter crashes, ensure bin/cli.js exists and package.json scripts are correct.
Troubleshooting
- No
.envfile:- Run
npx genv-ex --initto create a sample. - Or create
.envmanually withAPI_KEY=your_key.
- Run
- File not found:
- Verify
bin/cli.jsexists ingenv-ex/bin/. - Check
package.json’sbinandscripts.
- Verify
- Nodemon crashes:
- Run
node bin/cli.jsto isolate errors. - Ensure script points to
bin/cli.js, notsrc/cli.js.
- Run
- Permissions:
- Run
chmod +x bin/cli.js. - Check write access:
chmod u+w ..
- Run
- Dependencies:
npm install dotenv json5 commander npm install --save-dev nodemon
Contributing
Contributions are welcome! To contribute:
- Fork the repository.
- Create a branch:
git checkout -b feature/your-feature. - Commit changes:
git commit -m "Add your feature". - Push:
git push origin feature/your-feature. - Open a pull request.
Please include tests and update this README.md.
License
Licensed under the MIT License. See LICENSE for details.
