Work With better-env In A Repo
Type-safe environment config modules
Follow this best practice to manage environment variables in TypeScript applications with full type safety and clear server/public boundaries.
exports
to define typed env modules and recommends placing them in feature-level
files (for example
and
src/lib/database/config.ts
).
Learn more:
references/config-schema.md
Validate existence of all env variables in the current environment
Run env validation early so missing or invalid values fail fast before
,
, or deploy steps.
better-env validate --environment <name>
loads
files with Next.js semantics, discovers
modules, and checks every declared variable from your
modules.
If your dotenv files intentionally include keys that are not referenced by config modules, add per-env suppressions in
:
environments.<env>.ignoreUnused: string[]
These suppress only the selected local environment during
.
Adapter defaults are merged in automatically; for Vercel,
is ignored by default in
,
, and
.
Learn more:
references/env-validation.md
Configure runtime syncing between local files and hosted providers
Use runtime configuration to keep local dotenv targets aligned with provider environments while preserving safe defaults.
Create
with
and an adapter (
,
,
, or
), then define environment mappings, env-file targets, and gitignore behavior.
Learn more:
Use the CLI for day-to-day environment operations
The CLI gives a consistent workflow for initialization, sync, validation, and remote variable management, which is great for local development and CI automation.
Choose the command runner to match the repo:
- Use in npm/pnpm-based repos (for example lockfiles like or and scripts run via /).
- Use in Bun-based repos (for example and scripts run via ).
- Keep commands aligned with the project's existing package manager/runtime conventions; do not mix runners unless the repo already does.
Recommended flow in a repo:
- Run once to verify adapter prerequisites.
- Run
better-env pull --environment <name>
to sync local env files.
- Run
better-env validate --environment <name>
before app startup/build.
- Use , , , , and for remote env changes.
Choose command behavior intentionally:
- for idempotent automation and scripts
- when duplicate keys should fail
- when missing keys should fail
- to remove remote keys
- for batch updates from dotenv files
Learn more:
references/vercel-adapter.md