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
serverandclientschemas, plussharedfor variables read on both sides, such asNODE_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
| Import | Exports | Notes |
|---|---|---|
zod-config/nextjs | loadEnvConfig | Prefix fixed to NEXT_PUBLIC_. runtimeEnv needs one entry per client and shared key, checked by the types. |
zod-config | loadEnvConfig | Pass the prefix as clientPrefix, e.g. "VITE_" or "PUBLIC_". runtimeEnv can be any object, e.g. import.meta.env. |
zod-config/nextjs/config | withZodConfigEnv | Node.js only. Validates the env file when Next.js reads its config. |
zod-config/vite | zodConfigEnv | Vite 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;Where values come from
servervariables are read fromruntimeEnvfirst, thenprocess.env, and only on the server. In the browser they are never read, and accessing one throws.clientvariables are read fromruntimeEnvonly, on the server and in the browser.sharedvariables are read likeservervariables on the server, and fromruntimeEnvin 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:
| Framework | How |
|---|---|
| Next.js | Import the env file in next.config.ts, or wrap the config with withZodConfigEnv |
| Vite, TanStack Start, Astro | Add 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 asserver-only, or code that needs the browser, fails there. - jiti is an optional peer dependency, version 2 or later. Path aliases from
tsconfig.jsonneed jiti 2.7 or later, and only the roottsconfig.jsonis read, not files it references such astsconfig.app.json. On Vite,resolve.aliasworks too. - Vite restarts the dev server when
env.tsor a.envfile changes, not when a file thatenv.tsimports 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 NaNMisconfiguration 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
| Option | Description |
|---|---|
server, client | Objects of Zod schemas. At least one group is required. |
shared | Variables 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 }). |
clientPrefix | zod-config only. Required and non-empty when client has keys. |
runtimeEnv | Where 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. |
skipValidation | Return 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. |
isServer | Override server detection (default typeof window === "undefined"), e.g. in jsdom tests. |
strict | zod-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.