next-safe-env ships a CLI you can run with npx - no install required.
check - validate before you deploy
Validates the current environment against your schema without starting the app. Useful in CI pipelines to gate deployments before they reach production.
Arguments
| Argument | Default | Description |
|---|---|---|
file | src/env.js, then dist/env.js | Path to the compiled env file to validate |
Exit codes
| Code | Meaning |
|---|---|
0 | All environment variables are valid |
1 | One or more variables are missing or invalid |
createEnv runs at import time, any validation failure triggers the same formatted error output you’d see at startup - then the process exits with code 1.
Examples
Sample output on failure
check imports your compiled .js file - not the .ts source. Build your project first (npm run build) or use a TypeScript runner like tsx:init - interactive scaffold
Generates src/env.ts and .env.example by asking which variables your app needs and what type each one should be.
Options
| Option | Default | Description |
|---|---|---|
--output <path> | src/app/env.ts (Next.js App Router), app/env.ts (Next.js Pages Router), src/env.ts (all others) | Output path for the generated env.ts |
What it asks
- Framework - auto-detected from
package.json(checks fornext,vite,@cloudflare/workers-types, etc.). If detected, you’re asked to confirm; if not, you choose fromNext.js,Node.js,Vite, orEdge Runtime. The framework determines the adapter and client-var prefix. - Server-side variables - name, type, optional flag, default value, and (for
str) allowed values - Client-side variables - same prompts; variable names are auto-prefixed (
NEXT_PUBLIC_orVITE_) if you omit the prefix - Output file path
- Whether to generate
.env.example
Example session
If the adapter cannot be auto-detected, the framework menu is shown instead:
Generated src/app/env.ts
.env.example generation
Running init with the .env.example prompt answered y produces a commented template alongside src/env.ts. Each variable gets an inline comment describing its type, whether it is required, any constraints, and its default value - so new contributors know exactly what to fill in.

