Team conventions for environment variables in a Next.js app with T3 Env: separate server and client schemas, NEXT_PUBLIC_ only for public values, coerced booleans and numbers, defaults, no process.env outside the env module, env in tests. Use when adding, reading or reviewing an environment variable or the env module. Background knowledge that agents preload or load on demand; not a command.
Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
SSkills DirectorySkills Directory
Is that skill safe to install?
Are you the author of Env Rules?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/janszewczyk-env-rules)
---
name: env-rules
description: "Team conventions for environment variables in a Next.js app with T3 Env: separate server and client schemas, NEXT_PUBLIC_ only for public values, coerced booleans and numbers, defaults, no process.env outside the env module, env in tests. Use when adding, reading or reviewing an environment variable or the env module. Background knowledge that agents preload or load on demand; not a command."
user-invocable: false
---
<!-- generated by knowledge.mjs from 8 note(s) (sha256 302d00c71d61); edit the notes in knowledge/, then run build -->
# Environment variable rules (T3 Env)
Apply these rules whenever a task adds, reads or changes an environment variable. The first section is always in force; open a rule from the table when the task touches it. Setting the env module up is in `setup-app-infrastructure`.
## Always apply (critical, high)
### Never put a secret in a NEXT_PUBLIC_ variable or in the client schema
_critical · typescript, react, nextjs, t3-env_
**Rule.** Declare a `NEXT_PUBLIC_` variable only in the `client` schema of `data/env/client.ts`, and only for a value meant to be public: a publishable key, a public URL, an analytics id. A secret key, token, private key or connection string goes in the `server` schema without the prefix and is read only in server code.
**Why.** Next.js inlines every `NEXT_PUBLIC_` value into the JavaScript sent to the browser at build time, so a secret given the prefix is published to every visitor and must be rotated.
**Incorrect:**
```ts
// data/env/client.ts
client: {
NEXT_PUBLIC_STRIPE_SECRET_KEY: z.string().startsWith("sk_"),
},
```
**Correct:**
```ts
// data/env/server.ts
server: { STRIPE_SECRET_KEY: z.string().startsWith("sk_") },
// data/env/client.ts
client: { NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY: z.string().startsWith("pk_") },
```
### Convert numbers and booleans in the env schema, never with z.coerce.boolean
_high · typescript, react, nextjs, t3-env, zod_
**Rule.** Turn every non-string variable into its type in the schema: `z.coerce.number()` with its bounds for numbers, `z.stringbool()` (Zod 4) for flags, a `.transform` on a string for lists. Never use `z.coerce.boolean()` and never parse an env value at the call site.
**Why.** `z.coerce.boolean()` turns every non-empty string into `true`, so `FEATURE_EXPORT=false` switches the feature on; parsing at the call site repeats the conversion and skips validation.
**Correct:**
```ts
server: {
FEATURE_EXPORT: z.stringbool().default(false),
RATE_LIMIT_MAX: z.coerce.number().int().positive().default(100),
},
// call site
const max = env.RATE_LIMIT_MAX;
```
### Read environment variables only through the T3 Env object, never through process.env
_high · typescript, react, nextjs, t3-env_
**Rule.** Declare every environment variable in the `createEnv` schema of `data/env/server.ts` or `data/env/client.ts` and read it as `env.NAME`. `process.env` appears only inside those two files.
**Why.** A raw `process.env.X` read is unvalidated and typed `string | undefined`, so a missing or misspelled variable passes the build and surfaces as `undefined` in production; it also skips the server/client split that keeps server values out of client code.
**Correct:**
```ts
import { env } from "~/data/env/server";
const stripe = new Stripe(env.STRIPE_SECRET_KEY);
```
**Exceptions.** Tool configuration that runs outside the app (`vitest.config.ts`, `playwright.config.ts`) may read `process.env`, for example `process.env.CI`.
### Set SKIP_ENV_VALIDATION only for lint, tests and Docker image builds
_high · typescript, react, nextjs, t3-env_
**Rule.** Wire `skipValidation: !!process.env.SKIP_ENV_VALIDATION` into both env files and set the variable only where the real environment is absent on purpose: lint and type-check, unit tests, and the build stage of a Docker image whose variables arrive when the container starts. Never set it in a running deployment or in a CI build that ships.
**Why.** With validation skipped, `createEnv` returns the raw environment: a missing variable is `undefined`, no default applies and no transform runs, so `ANALYZE="false"` stays a truthy string and the app runs on wrong values instead of refusing to start.
**Correct:**
```dockerfile
FROM deps AS build
# The server environment is read when the container starts, not at build.
ENV SKIP_ENV_VALIDATION=true
RUN npm run build
FROM node:22-alpine AS runner
ENV NODE_ENV=production
CMD ["node", "server.js"]
```
**Exceptions.** Skipping at the image build suits server variables only: Next.js inlines `NEXT_PUBLIC_` values during `next build`, so public variables must be present then (for example as build arguments), not first set when the container starts.
### Split env by side: data/env/server.ts for server code, data/env/client.ts for client code
_high · typescript, react, nextjs, t3-env, zod_
**Rule.** Declare server-only variables in `data/env/server.ts` (`server` schema, `experimental__runtimeEnv: process.env`) and public ones in `data/env/client.ts` (`client` schema, each variable mapped by name in `experimental__runtimeEnv`). Server Components, server actions and route handlers import `~/data/env/server`; `"use client"` files import only `~/data/env/client`.
**Why.** One shared env file ships every server variable name to the browser, and a client component that reads a server variable from it throws at runtime. Next.js inlines a public variable only where `process.env.NEXT_PUBLIC_X` is written out, so the client file must map each one explicitly.
**Correct:**
```ts
// data/env/client.ts
export const env = createEnv({
client: { NEXT_PUBLIC_GA_ID: z.string().optional() },
experimental__runtimeEnv: { NEXT_PUBLIC_GA_ID: process.env.NEXT_PUBLIC_GA_ID },
emptyStringAsUndefined: true,
skipValidation: !!process.env.SKIP_ENV_VALIDATION,
});
// components/analytics.tsx ("use client")
import { env } from "~/data/env/client";
```
## Read when the task touches it
| Impact | Rule | Applies to |
| --- | --- | --- |
| medium | [Let an empty or missing optional variable fall back to a default declared in the schema](references/rules.md#let-an-empty-or-missing-optional-variable-fall-back-to-a-default-declared-in-the-schema) — emptyStringAsUndefined: true in both env files and .default() on optional variables; never env.X \|\| "...". | typescript, react, nextjs, t3-env, zod |
| medium | [List every schema variable in a committed .env.example with a placeholder value](references/rules.md#list-every-schema-variable-in-a-committed-envexample-with-a-placeholder-value) — Adding a variable to data/env/*.ts adds it to .env.example too; real values live in the git-ignored .env.local. | typescript, react, nextjs, t3-env |
| medium | [In tests, skip env validation in setup and stub variables with vi.stubEnv before import](references/rules.md#in-tests-skip-env-validation-in-setup-and-stub-variables-with-vistubenv-before-import) — SKIP_ENV_VALIDATION in the Vitest setup; vi.stubEnv, vi.resetModules, then a dynamic import; never set process.env. | typescript, react, nextjs, t3-env, vitest |