better-env
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseWork With better-env In A Repo
在代码仓库中使用better-env
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.
better-envconfigSchemaconfig.tssrc/lib/auth/config.tssrc/lib/database/config.tsLearn more:
references/config-schema.md
遵循此最佳实践,在TypeScript应用中管理环境变量,同时具备完整的类型安全性和清晰的服务端/客户端边界。
better-envconfigSchemaconfig.tssrc/lib/auth/config.tssrc/lib/database/config.ts了解更多:
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.
devbuildbetter-env validate --environment <name>.env*src/lib/*/config.tsconfigSchemaIf your dotenv files intentionally include keys that are not referenced by config modules, add per-env suppressions in :
better-env.tsenvironments.<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 .
validateVERCEL_OIDC_TOKENdevelopmentpreviewproductionLearn more:
references/env-validation.md
尽早运行环境验证,确保缺失或无效的值在、或部署步骤开始前快速报错。
devbuildbetter-env validate --environment <name>.env*src/lib/*/config.tsconfigSchema如果你的dotenv文件中包含未被配置模块引用的键值对,可以在中添加针对特定环境的忽略规则:
better-env.tsenvironments.<env>.ignoreUnused: string[]这些规则仅在命令执行时对指定的本地环境生效。适配器默认规则会自动合并;以Vercel为例,在、和环境中会被默认忽略。
validateVERCEL_OIDC_TOKENdevelopmentpreviewproduction了解更多:
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.
better-env.tsdefineBetterEnv(...)vercelAdapternetlifyAdapterrailwayAdaptercloudflareAdapterLearn more:
references/config.mdreferences/runtime.md
使用运行时配置保持本地dotenv目标与平台环境一致,同时保留安全的默认值。
通过创建并配置适配器(、、或),然后定义环境映射、env文件目标以及gitignore规则。
defineBetterEnv(...)better-env.tsvercelAdapternetlifyAdapterrailwayAdaptercloudflareAdapter了解更多:
references/config.mdreferences/runtime.md
Use the CLI for day-to-day environment operations
使用CLI进行日常环境操作
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
npxorpackage-lock.jsonand scripts run viapnpm-lock.yaml/npm).pnpm - Use in Bun-based repos (for example
bunxand scripts run viabun.lock).bun - 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.
better-env init - Run to sync local env files.
better-env pull --environment <name> - Run before app startup/build.
better-env validate --environment <name> - Use ,
add,upsert,update, anddeletefor remote env changes.load
Choose command behavior intentionally:
- for idempotent automation and scripts
upsert - when duplicate keys should fail
add - when missing keys should fail
update - to remove remote keys
delete - for batch updates from dotenv files
load
Learn more:
references/cli.mdreferences/vercel-adapter.md
CLI提供了初始化、同步、验证和远程变量管理的统一工作流,非常适合本地开发和CI自动化场景。
根据代码仓库选择对应的命令运行器:
- 在基于npm/pnpm的仓库中使用(例如包含
npx或package-lock.json锁文件,且脚本通过pnpm-lock.yaml/npm运行)。pnpm - 在基于Bun的仓库中使用(例如包含
bunx锁文件,且脚本通过bun.lock运行)。bun - 保持命令与项目现有的包管理器/运行时约定一致;除非仓库本身已经混用,否则不要混合使用不同的运行器。
推荐的仓库使用流程:
- 运行一次以验证适配器的前置条件。
better-env init - 运行同步本地环境文件。
better-env pull --environment <name> - 在应用启动/构建前运行。
better-env validate --environment <name> - 使用、
add、upsert、update和delete命令管理远程环境变量。load
根据需求选择合适的命令:
- :适用于幂等自动化和脚本场景
upsert - :当重复键值应触发报错时使用
add - :当缺失键值应触发报错时使用
update - :删除远程键值
delete - :从dotenv文件批量更新
load
了解更多:
references/cli.mdreferences/vercel-adapter.md