Environment

Declare the environment variables the application reads in src/env.ts, with field. The server’s are read from env and the public ones from import.meta.env, both as the declarations read them.

On this page

Declarations

src/env.ts
import { field } from '@k8ordo/field';

export const server = field.object({
  DATABASE_URL: field.url({ required: true }),
  DATABASE_TOKEN: field.string({ required: true }),
});

export const client = field.object({
  VITE_PAGE_SIZE: field.integer({ min: 1, default: 20 }),
});

Export two declarations, server and client, each a field.object({ … }) built with field from @k8ordo/field. Both modes read them the same way.

The name decides which side a variable is on. One starting with Vite’s envPrefix (VITE_ by default) is public, and goes in client. Any other is the server’s, and goes in server. Each declaration is given the variables of its own side only.

Information

Note

Declaring a variable that starts with VITE_ in server is an error before anything is read: Vite hands its value to the browser too, so it can never be the server’s alone. So is a field.list() or a field.object() on either side, which one string cannot hold.

How a variable is read

A variable holds one string, which the declared kind reads, with the same kinds and constraints a route parameter takes. field.integer() reads it as a number, and field.boolean() reads only true or false.

An unset variable reads as its default. Without one, a required variable is refused, and any other reads as its kind’s empty value: '' for a text kind, false for field.boolean(), and undefined for the rest.

An empty variable, NAME=, reads as '' in a text kind. Only required refuses it: like HTML’s minlength, minLength never refuses an empty string. In any other kind, such as a number or a boolean, an empty variable reads as unset.

Server variables

src/lib/database.ts
import 'server-only';

import { env } from '@k8ordo/framework/env';
import { createClient } from '@libsql/client';

export const database = createClient({
  url: env.DATABASE_URL,
  authToken: env.DATABASE_TOKEN,
});

env holds what the server declaration read: a number, with field.integer(). It is read-only, and typed by the declaration.

Code the handler runs reads it: a page, a layout, a route.ts, a Server Action. The handler reads the environment before any module of the application, so a module’s top level sees what the declaration read too.

text
@k8ordo/framework/env holds the server's environment, and a module the browser loads cannot read it — these are, and import it:
  src/components/settings.tsx
  src/lib/database.ts

A 'use client' module, or a module one imports, that imports @k8ordo/framework/env fails the build, which names every such module. Under vite dev the module fails as it is compiled.

Reading env outside the handler fails with env is read where the request handler has not loaded src/env.ts. Test code that reads it through the built handler.

Warning

Pitfall

Props passed to a client component end up in the HTML and the payload. Pass a server variable that way only when the visitor may see it.

Public variables

src/components/page-size.tsx
'use client';

export function PageSize() {
  const size = import.meta.env.VITE_PAGE_SIZE;
  return <p>{`${size} per page`}</p>;
}

Public variables are read from Vite’s import.meta.env. The framework reads them through the client declaration and puts what it read in place of each string, so VITE_PAGE_SIZE is a number in the browser and on the server alike. A variable the declaration reads as undefined is written in as undefined.

Warning

Pitfall

A public variable is in the bundle. Never put a secret in one.

Types

.k8ordo/register.gen.ts
declare module '@k8ordo/framework/env' {
  interface Register {
    env: Produced<typeof server>;
  }
}

declare global {
  interface ImportMetaEnv extends Readonly<Produced<typeof client>> {}
  interface ViteTypeOptions {
    strictImportMetaEnv: unknown;
  }
}

The generated .k8ordo/register.gen.ts types env and import.meta.env by what the declarations read. Exporting anything but field.object({ … }) is a type error in that file. Without the generated types, every name read from env is a type error saying no server declaration is registered.

A number, an enum, a date or a time with neither default nor required may hold nothing, so its type includes undefined.

With src/env.ts in place, reading a public variable it does not declare is a type error. Vite’s own, such as MODE and DEV, are read as before.

k8ordo generate writes the types without reading the environment, so CI type-checks without the values.

When they are read

The declarations are read once, when the values are known, never per request.

  • vite dev: as it starts, .env files included. When the values do not satisfy the declarations the server keeps running, and answers every request with a 500 saying why.
  • Static mode: both at build time, .env files included. A variable its declaration refuses stops the build, which names it.
  • Server mode: the public variables at build time. The server’s are read from process.env as the handler loads where it is deployed. No .env file is read in production.
bash
node --env-file=.env dist/server.js

To use a .env file in production, load it into process.env with Node.js’s --env-file.

When the server’s variables do not satisfy the declaration, the handler does not load: node dist/server.js exits with 1, naming each variable.

On Cloudflare Workers, process.env holds the Worker’s variables under nodejs_compat with a compatibility date of 2025-04-01 or later. Under Deno, allow the names the declaration reads with --allow-env.

Running the handler on another runtime is covered in Deployment.

Information

Note

No error message carries a value. Each names the variable and why it was refused, as in DATABASE_URL: is missing.

k8ordo

React libraries that use Baseline features without holding back.