Modules, Runtime & Tooling
Environment Variables & Config
Configuration can exist at build time, server runtime, client runtime, and test runtime. In browser bundles, exposed environment variables are public values baked into shipped JavaScript.
- Build-time config is substituted into output
- Server runtime config is read by the process
- Client config is visible to users
- Config should be validated
- Tests need isolated config
| Location | When resolved | Visible to |
|---|---|---|
| Build-time client env | During build | Anyone with the bundle |
| Server env | At process runtime | Server process |
| Test env | During test run | Test process |
| Runtime public config endpoint | When client loads | Anyone who calls endpoint |
| Secrets manager | At deploy/runtime | Authorized server code |
function requiredEnv(name: string): string {
const value = process.env[name]
if (!value) throw new Error(`Missing environment variable: ${name}`)
return value
}
export const config = {
databaseUrl: requiredEnv("DATABASE_URL"),
apiSecret: requiredEnv("API_SECRET"),
}