A small Node.js CLI that scans your codebase for process.env and import.meta.env usage and generates a .env.example file with all discovered variables.
- Multi-runtime: Detects both
process.env.VAR_NAME(Node) andimport.meta.env.VAR_NAME(Vite, etc.) - Multiple languages: Scans
.js,.ts,.jsx,.tsx,.mjs,.cjs,.vue,.svelte - Sensible ignores: Skips
node_modules,.git,dist,build,.next, and other common output/cache dirs - Report + file: Prints a report of variables and where they’re used, then writes
.env.example - Non-destructive: If a
.envexists, the script only notes that values exist; it does not overwrite.env
- Node.js 14+ (uses only built-in
fsandpath; no dependencies)
# Scan current directory and generate .env.example
node index.js
# Scan a specific directory
node index.js /path/to/your/projectOr make it executable and run directly:
chmod +x index.js
./index.js
./index.js /path/to/project- Console: A list of unique env variable names and the files/lines where they appear.
- File: A new or updated
.env.examplein the target directory with:- One entry per variable as
VAR_NAME= - Comments indicating which files use each variable
- One entry per variable as
Given code that uses process.env.API_URL and import.meta.env.VITE_APP_KEY, running:
node index.js .produces a report like:
🔍 Scanning: /path/to/project
Found 2 unique env variables across 5 files:
────────────────────────────────────────────────────────────
API_URL
→ src/api.js:12
VITE_APP_KEY
→ src/config.ts:3
────────────────────────────────────────────────────────────
Generated .env.example with 2 variables
/path/to/project/.env.example
And a .env.example similar to:
# ============================================
# Environment Variables
# Auto-generated by scan-env.js on 2025-02-14
# ============================================
# Used in: src/api.js
API_URL=
# Used in: src/config.ts
VITE_APP_KEY=The scanner skips: node_modules, .git, dist, build, .next, .nuxt, coverage, .turbo, .cache.
Use and modify as you like.