Skip to main content
next-safe-env is a zero-dependency library that validates and types your environment variables at startup. You define a schema once - server vars, client vars, their types, and any constraints - and the library either hands you a fully-typed, frozen object or crashes loudly with every problem listed, before your app serves a single request.

The problem with process.env

In every TypeScript project, process.env.X is always string | undefined. There is no autocomplete, no type coercion, and nothing that stops you from reaching a missing or malformed variable deep inside a request handler. The standard workaround is hand-written guard code repeated across every file that needs an env var:
This approach fails in compounding ways: missing vars are discovered mid-request instead of at startup, there is no single place to audit what your app needs to run, and nothing prevents you from reading a server-only secret in a client component where it silently becomes undefined in the browser. next-safe-env replaces all of that with a single function call:

Key features

Startup crash, never runtime crash

Every variable is validated synchronously at module load time. If anything is missing or malformed, the process exits with a readable error listing every problem at once.

Full TypeScript inference

The return type of createEnv() is fully inferred from your schema. No separate type definitions to maintain. Add a field and it appears everywhere automatically.

Server/client split

Server vars and client vars live in separate schema keys. The nextjs adapter enforces the NEXT_PUBLIC_ prefix and strips server vars from the browser bundle at runtime.

Zero dependencies

No Zod, no dotenv, no peer deps. All validation logic ships in under 5 kB gzipped. Zod interop is available if your project already uses it - but it is never required.

Automatic type coercion

Built-in helpers like num() and bool() automatically convert string environment variables to their correct types so "3000" becomes 3000 and "true" becomes true, without any manual parsing.

Multi-runtime support

Works in Next.js App Router, plain Node.js, Vercel Edge Runtime, Next.js Middleware, and Vite. The correct adapter is auto-detected from the runtime environment.

How it compares

The table below uses data from the project README to compare next-safe-env against the most common alternatives. If your project already uses Zod and you want schema reuse, t3-env integrates tightly with it. next-safe-env is the right choice when you want the full feature set - typed vars, server/client splitting, Edge Runtime support, and pretty error output - without adding any runtime dependencies.

Requirements

  • Node.js 18 or later
  • TypeScript 5.x
  • No runtime dependencies
next-safe-env validates what is already in process.env. It does not load .env files. Use Next.js’s built-in .env support or dotenv to load your files first, then let next-safe-env validate and type the result.

Next step

Quick Start

Install next-safe-env and define your first typed environment schema in under 5 minutes.