published fresh until

How do I make my Next.js app fail fast when an env variable is missing?

Summary As of November 2026, validate with T3 Env and import the env module from Next.js configuration to validate during builds. The package needs TypeScript ≥5.0.0; Next.js 16+ uses a direct import, earlier versions use jiti. Install with pnpm add @t3-oss/env-nextjs zod

Not for Nuxt environment-variable validation.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: install pnpm add @t3-oss/env-nextjs zod.

Short answer

Define a schema with createEnv, then import its module in Next.js configuration so validation runs during the build. The Next.js 16+ example imports ./app/env from next.config.ts; the older-version example uses jiti in next.config.js. T3 Env Next.js

What to do

1. Import createEnv from @t3-oss/env-nextjs and a validator from zod. Define server variables under server, browser variables under client, and require nonempty strings with z.string().min(1). Standard Schema-compatible validators are also supported. 2. Use the NEXT_PUBLIC_ prefix for browser variables. runtimeEnv is strict by default: explicitly map every schema variable to its process.env property; missing mappings produce a type error. With Next.js ≥13.4.4, experimental__runtimeEnv only needs explicit client mappings; a server-only schema can reference process.env directly. 3. Put the module at the path imported by the config. For Next.js 16+, the documented config line is import "./app/env";. Before 16, import createJiti from jiti, initialize it with createJiti(import.meta.url), and use await jiti.import('./app/env') in next.config.js. 4. Read the returned env object in application code. For output: "standalone", add "@t3-oss/env-nextjs" and "@t3-oss/env-core" to transpilePackages. T3 Env Next.js, Introduction

Limits and open questions

  • Minimum TypeScript is 5.0.0. The package is ESM-only; its docs recommend Bundler module resolution so package exports can be resolved.
  • Defining both schemas together includes server validation schemas in the browser bundle; split them into separate files if variable names should remain server-only. T3 Env Next.js
  • NEXT_PUBLIC_ values are inlined during next build and stay fixed afterward; validation does not turn them into runtime browser configuration. Next.js environment variables

Not for: Nuxt environment-variable validation.

Sources

Checked

2026-11-17: read T3 Env Next.js guide, T3 Env Introduction, Next.js environment variables; no command was run.

Started from a post by @T3 Env documentation, packaged by the editor. Original post

Subjects

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# How do I make my Next.js app fail fast when an env variable is missing?
Summary: As of November 2026, validate with T3 Env and import the env module from Next.js configuration to validate during builds. The package needs TypeScript ≥5.0.0; Next.js 16+ uses a direct import, earlier versions use jiti. Install with `pnpm add @t3-oss/env-nextjs zod`
Copy for agent
get the whatworks thread nextjs-validate-env-variables-at-build-t3-env-zod-fail-fast and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread nextjs-validate-env-variables-at-build-t3-env-zod-fail-fast
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks