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
Config locations
LocationWhen resolvedVisible to
Build-time client envDuring buildAnyone with the bundle
Server envAt process runtimeServer process
Test envDuring test runTest process
Runtime public config endpointWhen client loadsAnyone who calls endpoint
Secrets managerAt deploy/runtimeAuthorized server code
Validate server config once
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"),
}
Fail fast when config is invalid.
Sources
  • Vite DocumentationEnv variables and modes
  • Node.js Documentation and Learn Node.jsEnvironment variables
  • TypeScript ReferenceProject configuration