Skip to content

flyingsquirrel0419/dotenv-scan

🩺 dotenv-scan

One command to catch every missing, unused, and undocumented .env variable.

npm version License Node


Why dotenv-scan?

Every developer has deployed to production only to watch it crash because of a missing .env variable. dotenv-scan scan finds those variables before they cause downtime β€” and cleans up stale ones while it's at it.

Three problems, one command:

Problem What happens How dotenv-scan helps
Missing variables Code uses DATABASE_URL but .env doesn't have it Detected as ❌ Missing
Unused variables .env has OLD_REDIS_URL nobody uses anymore Detected as πŸ—‘οΈ Unused
Undocumented variables .env has INTERNAL_API_KEY but .env.example doesn't Detected as ⚠️ Undocumented

✨ Features

  • πŸš€ Zero config β€” run npx dotenv-scan in any project directory
  • πŸ” Multi-language β€” JavaScript, TypeScript, Python, Go, Ruby
  • πŸ“Š Structured output β€” pretty terminal tables or JSON for tooling
  • πŸ” Auto-generate .env.example β€” keeps docs in sync with one command
  • πŸ€– CI-ready β€” dotenv-scan check exits with code 1 on missing variables
  • ⚑ Fast β€” scans hundreds of files in under 500ms

πŸš€ Quick Start

npx dotenv-scan scan

That's it. No install needed.

dotenv-scan v1.0.0  Β·  scanned 47 files in 284ms

❌  Missing  (3)
   DATABASE_URL      src/db.ts:12, src/config.ts:8
   JWT_SECRET        src/auth/middleware.ts:4
   STRIPE_API_KEY    src/payments/stripe.ts:22

⚠️  Undocumented  (2)
   INTERNAL_API_KEY
   DEBUG_MODE

πŸ—‘οΈ  Unused  (1)
   OLD_REDIS_URL

βœ…  OK  (8)
   PORT, NODE_ENV, API_BASE_URL, ... (and 5 more)

────────────────────────────────────────
Run `dotenv-scan generate` to update .env.example

πŸ“¦ Installation

Run without installing (recommended):

npx dotenv-scan scan

Install globally:

npm install -g dotenv-scan

From source:

git clone https://github.com/dotenv-scan/dotenv-scan
cd dotenv-scan && npm install && npm run build

πŸ“– Commands

dotenv-scan scan

Scan and report env variable issues. Always exits 0.

dotenv-scan scan                          # scan current directory
dotenv-scan scan --dir ./src              # scan specific directory
dotenv-scan scan --dotenv .env.local    # use specific .env file
dotenv-scan scan --format json            # JSON output
dotenv-scan scan --no-color               # disable colors
dotenv-scan scan -i "**/vendor/**"        # ignore additional patterns

dotenv-scan check

Same as scan, but exits 1 if missing variables found. Perfect for CI.

dotenv-scan check                         # fails on missing vars
dotenv-scan check --strict                # also fail on unused/undocumented

CI example (GitHub Actions):

- name: Check env variables
  run: npx dotenv-scan check

dotenv-scan generate

Generate or update .env.example from scan results.

dotenv-scan generate                      # creates/updates .env.example
dotenv-scan generate --example .env.tmpl  # custom output path

βš™οΈ Options

Flag Short Default Description
--dir <path> -d ./ Directory to scan
--dotenv <path> -e .env Path to .env file
--example <path> -x .env.example Path to .env.example file
--ignore <patterns> -i β€” Additional glob patterns to ignore
--format <type> -f text Output format: text or json
--no-color β€” Disable colored output
--strict β€” (check only) Fail on unused/undocumented too
--version β€” Print version
--help β€” Print help

πŸ—ΊοΈ Supported Languages

Language Patterns detected
JavaScript / TypeScript process.env.VAR, process.env['VAR'], process.env["VAR"]
Python os.environ.get('VAR'), os.environ['VAR'], os.getenv('VAR')
Go os.Getenv("VAR")
Ruby ENV['VAR'], ENV.fetch('VAR')

Dynamic access warning: Patterns like process.env[dynamicKey] can't be statically analyzed. dotenv-scan detects these and reports them as warnings.


πŸ—οΈ Architecture

CLI Entry (Commander.js)
    β”‚
    β”œβ”€β”€ Scanner ──── Walker (fast-glob) ──── Extractors (per-language regex)
    β”‚                                                    β”‚
    β”‚                                          EnvRef[] (used variables)
    β”‚
    β”œβ”€β”€ Parser ──── Dotenv parser ──── EnvDef[] (defined variables)
    β”‚
    └── Analyzer ── compares used ↔ defined ↔ documented
                         β”‚
                    AnalysisResult
                         β”‚
                    β”Œβ”€β”€β”€β”€β”΄β”€β”€β”€β”€β”
                 Reporter   Generator
                (text/json)  (.env.example)

πŸ§ͺ Development

git clone https://github.com/dotenv-scan/dotenv-scan && cd dotenv-scan
npm install
npm run build          # build first (required for integration tests)
npm test               # run test suite
npm run test:coverage  # run with coverage report
npm run lint           # check code style

See CONTRIBUTING.md for full guidelines.


πŸ—ΊοΈ Roadmap

  • v1.0 β€” Core scan, check, generate commands
  • v1.1 β€” PHP and Rust extractors
  • v1.2 β€” dotenv-scan.config.ts support
  • v2.0 β€” Monorepo multi-.env support, VSCode extension

🀝 Contributing

Contributions are welcome! See CONTRIBUTING.md.


πŸ“„ License

Apache-2.0 β€” see LICENSE for details.


Made with ❀️ for developers tired of "undefined is not a string" · Star this repo ⭐

About

🩺 One command to catch every missing, unused, and undocumented .env variable

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Packages

 
 
 

Contributors