safe-dotenv-check
v1.6.0
Published
Validate .env files against .env.example with schema rules, env-specific checks, and CI-friendly output.
Downloads
107
Maintainers
Readme
safe-dotenv-check
safe-dotenv-check is a small CLI for keeping .env.example honest.
It checks real env files against the contract you meant to keep in the repo: required keys, optional notes, warning-only integrations, type rules, env-specific requirements, and CI output that points at the actual mismatch.
npx safe-dotenv-check --example .env.example --env .env.production --env-name productionFAIL .env.production (production)
missing: OPENAI_API_KEY
invalid: DATABASE_URL (type=url), NODE_ENV (enum=development|staging|production)
next:
- add OPENAI_API_KEY to .env.production
- update DATABASE_URL to match type=urlWhy Use It
- Catch missing, blank, or badly shaped values before a deploy job gets that far.
- Keep
.env.exampleuseful instead of letting it drift away from.env.local,.env.ci, and production templates. - Mark noisy integrations as optional or warning-only while still documenting them.
- Use the same checks locally, in CI, and in GitHub Action summaries.
Bootstrap the contract from a real env file, with common schema hints inferred:
npx safe-dotenv-check --init --env .env.local --out .env.example --preset nextjsDATABASE_URL= # type=url desc="Primary database connection"
NEXT_PUBLIC_APP_URL= # type=url desc="Browser-exposed app URL"
NODE_ENV= # enum=development|test|production
PORT= # type=intSecret-like keys such as API keys, tokens, passwords, and private keys stay redacted and do not get value-based type guesses.
What It Checks
- required keys that are missing or empty
- optional keys that should be documented but not enforced
- warning-only keys that should be reported without failing deploys
- extra keys in target env files: fail, warn, or ignore
- value shape with
type=,enum=, andpattern= - env-specific contracts such as
env=production - manifest mistakes and overlapping duplicate keys with
--doctor - manifest drift across one or more env files with
--sync
Install
npm install --global safe-dotenv-checkOr run without installing:
npx safe-dotenv-check
npx safe-dotenv-check .env.production
npx safe-dotenv-check --example .env.example --env .env --format jsonBy default, the CLI uses .env.example and .env when both exist. Positional arguments are treated as target env files.
Manifest
Use .env.example as the contract:
DATABASE_URL= # type=url desc="Primary database"
OPENAI_API_KEY=
NODE_ENV=development # enum=development|staging|production
PORT=3000 # type=int
?SENTRY_DSN=
!SLACK_WEBHOOK_URL=
FEATURE_FLAGS={} # type=json optional
API_KEY= # pattern=^sk-[a-z0-9]+$Tiers:
KEY=means required: missing, empty, or invalid values fail.?KEY=or# optionalmeans documented only.!KEY=or# warnmeans report problems without changing the exit code.
Environment-specific rules:
?SENTRY_DSN= # env=dev desc="Local error tracking only"
SENTRY_DSN= # env=production desc="Production error tracking DSN"
!SLACK_WEBHOOK_URL= # env=staging,production desc="Deploy notifications"safe-dotenv-check --example .env.example --env .env.production --env-name productionSupported schema directives:
type=stringtype=intortype=integertype=numbertype=booleantype=urltype=jsonenum=value1|value2|value3pattern=<regex>
CLI
Common commands:
safe-dotenv-check
safe-dotenv-check .env.production
safe-dotenv-check --example .env.example --env .env --env .env.production
safe-dotenv-check --example .env.example --env .env.production --env-name production
safe-dotenv-check --example .env.example --env .env --extra warn
safe-dotenv-check --example .env.example --env .env --format json --redact-values
safe-dotenv-check --example .env.example --env .env --quiet
safe-dotenv-check --sync --example .env.example --env .env.local --env .env.production
safe-dotenv-check --doctor --example .env.exampleExit codes:
0: all enforced checks passed1: at least one target file failed2: invalid CLI usage or unreadable files
Output options:
--format text|json: choose human or machine output--redact-values: omit invalid raw values from JSON reports--show-descriptions: includedesc=text in reports--quiet: in text mode, print only failing or warning reports--no-suggestions: hide next-action hints--write-missing: alias for--sync --write--annotate: add source hints to generated or synced keys
Extra key modes:
safe-dotenv-check --example .env.example --env .env.production --extra fail
safe-dotenv-check --example .env.example --env .env.production --extra warn
safe-dotenv-check --example .env.example --env .env.production --extra ignoreGitHub Action
jobs:
env-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- uses: eunsujihoon-hub/[email protected]
with:
example: .env.example
env_files: |
.env.ci
.env.production
env_names: |
ci
production
extra: warnAction inputs:
example: manifest pathenv_files: newline-separated target env file pathsenv_names: optional newline-separated logical env namesextra:fail,warn, orignoreallow_extra: legacy alias forextra: ignoreshow_descriptions: include manifest descriptions in reportsredact_values: omit invalid raw values from JSON reports, defaults totruesummary: write a GitHub step summary, defaults totruejson_output_path: copy the JSON report to a chosen path
See docs/github-actions.md for a fuller setup.
Starter Helpers
Generate a redacted starter manifest from an existing local env file. Common schema directives are inferred from key names and values:
safe-dotenv-check --init --env .env.local --out .env.example
safe-dotenv-check --init --env .env.local --out .env.example --preset nextjs
safe-dotenv-check --init --env .env.local --out .env.example --preset node --annotateFind env keys that are missing from the manifest, across one file or several:
safe-dotenv-check --sync --example .env.example --env .env.local
safe-dotenv-check --sync --example .env.example --env .env.local --env .env.production
safe-dotenv-check --sync --example .env.example --env .env.local --write
safe-dotenv-check --write-missing --annotate --example .env.example --env .env.local--sync also reports keys that are still in .env.example but no longer appear in any checked env file. It does not remove them for you; stale entries usually need a human look.
Examples
Secret Safety
Commit redacted examples such as .env.example, not real secrets. This repo ignores common secret-bearing files including .env, .env.local, .env.production.local, .envrc, secrets/, *.pem, and *.key.
Development
npm test
npm run pack:checkBug reports and pull requests are welcome. If you hit a real env mismatch this tool should catch better, open an issue with the shape of the manifest and target env file.
