Skip to content

Environment variables ​

Declaring which variables a scope needs and how they validate.

package @smitejs/env

env.register(specs) is the declaration step: a map of name to a { key, validation } pair. This is where you answer "what does this scope require, and what shape is acceptable?" — exactly once, in one place. It returns a builder you finish with withProvider.

The spec is the contract ​

Each entry has two parts:

  • key — the raw provider key, i.e. the process.env variable name the provider is asked for ("DATABASE_URL").
  • validation — the zod schema driving everything: type coercion, optionality, defaults, and error messages.

The declared name is the property you read on the instance (databaseUrl), and the zod schema is also the type source — await appEnv.databaseUrl has the inferred type.

ts
const instance = env
  .register({
    databaseUrl: { key: "DATABASE_URL", validation: z.string() },
  })
  .withProvider(from({ DATABASE_URL: "postgres://x" }));
const url = await instance.databaseUrl;
ts
"postgres://x"

Required, optional, defaulted ​

  • z.string() — the variable must be present and a string.
  • z.string().optional() — a missing variable resolves to undefined.
  • z.coerce.number().default(3000) — missing variables fall back to the default; strings like "8080" are coerced to numbers.
ts
const instance = env
  .register({
    port: { key: "PORT", validation: z.coerce.number().default(3000) },
  })
  .withProvider(async () => undefined);
const port = await instance.port;
ts
3000

Build-time manifest ​

Each declared name is also recorded in a build-time manifest. The future CLI uses it to scaffold .env.example files and to fail the build on missing required variables at compile time instead of at boot.

Released under the MIT License.

/ search•[ ] prev/next•j/k scroll•h home•t theme•guide/techniques/env-variables