Next.js
Type-safe environment variables for Next.js with zod-config/nextjs.
zod-config/nextjs fixes the client prefix to NEXT_PUBLIC_.
Define the schema
import { } from 'zod-config/nextjs';
import { } from 'zod';
export const = ({
// server-only, read from process.env
: {
: .(), // Zod 3: z.string().url()
: ..().(3000),
},
// available in the browser, keys must start with NEXT_PUBLIC_
: {
: .(),
},
// read on both sides, without the prefix
: {
: .(['development', 'test', 'production']),
},
// one entry per client and shared key, so Next.js can inline the values
: {
: ..,
: ..,
},
});runtimeEnv needs one entry per client and shared key, written as process.env.NAME: Next.js only inlines a variable into the browser bundle where the code writes it out in full. The types check that every one is listed.
Use it
import { env } from '@/env';
env.PORT; // number
env.NEXT_PUBLIC_APP_URL; // string, on the server and in the browser
env.DATABASE_URL; // string on the server, throws in the browserAn invalid environment throws an Error that lists each invalid variable and why. See errors and options.
Validate when Next.js starts
By default, env.ts is checked the first time it is imported, so next dev starts even with a missing variable and only fails when a page uses it. To check it when next dev or next build starts, choose one of these options.
With next.config.ts and no path aliases such as @/ in env.ts. Nothing to install.
import './src/env';
import type { NextConfig } from 'next';
const nextConfig: NextConfig = {};
export default nextConfig;next buildfails before compiling, with the sameInvalid environment variablesmessage.next devreports the error and exits at startup.- Values from
.envfiles work: Next.js loads them before reading the config. - It works together with
skipValidation: withSKIP_ENV_VALIDATION=true, the build still runs.
To use the wrapper or jiti by hand, install jiti as a regular dependency, since next start needs it too:
npm install jitiDeploying
next start reads next.config too, so with any of the three options it validates again at start, and env.ts (plus jiti, for the wrapper) must be among the deployed files. With output: "standalone", the server does not read next.config at start: it needs neither, and the environment is validated on the first request that imports env.ts.
Monorepos
The wrapper looks for env.ts from the working directory. When you run next build apps/web from the repository root, pass an absolute path:
import { } from 'node:url';
import { } from 'zod-config/nextjs/config';
export default (
{},
{ : (new ('./src/env.ts', import..)) },
);jiti and tsconfig.json are then taken from the package that holds that file.
Building without secrets
When secrets are not available during next build, skip validation for the build:
SKIP_ENV_VALIDATION=true next buildexport const env = loadEnvConfig({
// ...
skipValidation: process.env.SKIP_ENV_VALIDATION === 'true',
});With skipValidation, raw values are returned without validation, coercion, transforms or defaults. The inferred types still describe the validated output, so they may not match the values.
Render pages that read these secrets at request time, for example with await connection() from next/server. Next.js renders static pages once, during next build. With validation skipped, a static page that reads a missing secret saves undefined into its HTML, and no error is shown.