Skip to Content
Zod Config

Type-safe environment variables

Validate server and client environment variables with Zod in Next.js, Vite, Astro and TanStack Start.

Validate environment variables with Zod in apps that have a browser bundle, such as Next.js, Vite, Astro and TanStack Start. loadEnvConfig gives you:

  • separate server and client schemas, plus shared for variables read on both sides, such as NODE_ENV;
  • public prefix enforcement: client keys must start with the framework's public prefix, server keys must not;
  • protection against reading server variables in the browser.

You write one env file. It is validated, synchronously, the first time it is imported. Next.js and Vite also get an optional helper that validates it when the dev server or the build starts.

Entry points

ImportExportsNotes
zod-config/nextjsloadEnvConfigPrefix fixed to NEXT_PUBLIC_. runtimeEnv needs one entry per client and shared key, checked by the types.
zod-configloadEnvConfigPass the prefix as clientPrefix, e.g. "VITE_" or "PUBLIC_". runtimeEnv can be any object, e.g. import.meta.env.
zod-config/nextjs/configwithZodConfigEnvNode.js only. Validates the env file when Next.js reads its config.
zod-config/vitezodConfigEnvVite plugin. Validates the env file when vite dev or vite build starts.
import {  } from 'zod';
import {  } from 'zod-config';

export const  = ({
  : 'PUBLIC_',
  : {
    : .(),
  },
  : {
    : .(),
  },
  : {
    : .(['development', 'test', 'production']),
  },
  : {
    : ..,
    : ..,
  },
});

.DATABASE_URL;
type DATABASE_URL: string

Where values come from

  • server variables are read from runtimeEnv first, then process.env, and only on the server. In the browser they are never read, and accessing one throws.
  • client variables are read from runtimeEnv only, on the server and in the browser.
  • shared variables are read like server variables on the server, and from runtimeEnv in the browser. They don't need the public prefix.

The server is detected with typeof window === "undefined". Override it with isServer, e.g. in jsdom tests.

Why runtimeEnv?

In the browser there is no process.env to read from. Bundlers put public values in the code themselves: Next.js replaces each process.env.NEXT_PUBLIC_* that the code writes out in full, and Vite puts them on import.meta.env, which only exists in your app's files. So your env file has to hand them over. Server values are read from process.env and don't need to be listed.

When validation runs

The environment is validated when the file that calls loadEnvConfig is first imported. In a single-page app, that is in the browser, when the app loads. To fail as soon as the dev server or the build starts:

FrameworkHow
Next.jsImport the env file in next.config.ts, or wrap the config with withZodConfigEnv
Vite, TanStack Start, AstroAdd the zodConfigEnv plugin

The plugin and the wrapper find env.ts (or env.mts, env.js, …) in src/, then in the project root, and run it with jiti. If the file is somewhere else, pass its path:

zodConfigEnv({ file: 'config/env.ts' });
withZodConfigEnv(nextConfig, { file: 'config/env.ts' });

What to know about the env file

  • It runs in Node.js when the plugin or the wrapper loads it. Keep it to the schema and loadEnvConfig: an import such as server-only, or code that needs the browser, fails there.
  • jiti is an optional peer dependency, version 2 or later. Path aliases from tsconfig.json need jiti 2.7 or later, and only the root tsconfig.json is read, not files it references such as tsconfig.app.json. On Vite, resolve.alias works too.
  • Vite restarts the dev server when env.ts or a .env file changes, not when a file that env.ts imports changes.

Errors

An invalid environment throws an Error that lists each invalid variable and why:

Error: Invalid environment variables:
  DATABASE_URL: Invalid input: expected string, received undefined
  PORT: Invalid input: expected number, received NaN

Misconfiguration also throws, before any validation: a client key without the prefix, a server key with it, the same key in two groups, client or shared keys without runtimeEnv or (on zod-config) client keys without clientPrefix, or no keys at all.

Options

OptionDescription
server, clientObjects of Zod schemas. At least one group is required.
sharedVariables read on the server and in the browser, without the public prefix, e.g. NODE_ENV. In the browser they come from runtimeEnv: list them on Next.js (NODE_ENV: process.env.NODE_ENV), or add them next to import.meta.env on Vite (runtimeEnv: { ...import.meta.env, NODE_ENV: process.env.NODE_ENV }).
clientPrefixzod-config only. Required and non-empty when client has keys.
runtimeEnvWhere values come from. Required when client or shared has keys. With zod-config/nextjs, list each client and shared key as process.env.NAME; server keys are optional. On the server, entries take priority over process.env, which provides the rest.
onError(error) => void, called with the Zod error (use error.issues) before the Error above is thrown. Throw from it to replace that error. Unlike loadConfig's onError, it does not swallow the failure.
skipValidationReturn raw values without running Zod, e.g. skipValidation: process.env.SKIP_ENV_VALIDATION === "true" when secrets are not available while building. Defaults, coercion and transforms are not applied, so the inferred types may not match the values. Reading server variables in the browser still throws.
isServerOverride server detection (default typeof window === "undefined"), e.g. in jsdom tests.
strictzod-config only. Type-checks that runtimeEnv lists every server, client and shared key and, when written inline, no undeclared key. For bundlers that only inline variables written out in full (e.g. process.env.EXPO_PUBLIC_API_URL in Expo). Type-level only.

When not to use it

loadEnvConfig is for apps with a browser bundle. For a server without one, use loadConfig or loadConfigSync with the env adapter, or any other adapter.

SvelteKit 3 is not supported: it has its own $app/env module and reserves src/env.ts.

On this page