Zod Global Configuration#
z.config() is the public API for reading and mutating Zod's global settings at runtime. Internally it operates on a single object stored at globalThis.__zod_globalConfig β the single source of truth regardless of how many copies of Zod are loaded in the same JavaScript realm (CJS, ESM, multiple bundles in a monorepo).
The $ZodConfig Shape#
The config object is typed as $ZodConfig and has five fields:
| Field | Purpose |
|---|---|
customError | Global error map. Runs after per-parse maps; overrides localeError. |
localeError | Lowest-priority error map, used by locale packages. |
jitless | Disables JIT schema compilation (needed in strict-CSP / no-eval environments). |
memoizer | Enables parsing of inputs that contain reference cycles. Read at schema construction time. |
postProcessor | Internal. Called on every newly constructed schema instance; set by import "zod/compile" for AOT compilation. Not part of the public API. |
How It Works#
At module load time, Zod initialises the shared object with a single ??= assignment :
(globalThis as GlobalThisWithConfig).__zod_globalConfig ??= {};
export const globalConfig = (globalThis as GlobalThisWithConfig).__zod_globalConfig!;
z.config(newConfig?) merges newConfig into that object via Object.assign and returns it . Because every loaded Zod copy points at the same globalThis property, one call updates all of them.
Pre-import bootstrap β because of the ??= guard, you can pre-populate the config before Zod even loads:
<script>globalThis.__zod_globalConfig = { jitless: true };</script>
<script type="module" src="app.js"></script>
Zod preserves that exact object identity on import.
Error Map Precedence#
customError and localeError slot into the global layers of Zod's error-resolution chain. From highest to lowest priority :
- Check-level error (
z.string().min(5, "Too short!")) - Schema-level error (
z.string("Invalid name")) - Per-parse error (
.parse(data, { error: β¦ })) customError(set viaz.config({ customError: β¦ }))localeError(set viaz.config(z.locales.en()))
Any error map may return undefined to defer to the next level in the chain.
Cross-Bundle Collision Risk#
Because globalThis.__zod_globalConfig is unversioned and shared, a third-party SDK that bundles its own copy of Zod and calls z.config({ customError: β¦ }) will silently overwrite the application's global error map β and vice versa.
This is the same trade-off already accepted for globalThis.__zod_globalRegistry. Mitigations:
- Prefer schema-level or per-parse error maps in library code; they have higher precedence and do not touch global state.
- If you must set
customErrorfrom a library, guard it to avoid clobbering an existing value: checkz.config().customErrorbefore assigning. - For
jitless, the flag is boolean and idempotent, so multiple writers are safe as long as they all agree on the value.
Key Source Files#
| File | What to look for |
|---|---|
packages/zod/src/v4/core/core.ts | $ZodConfig interface, globalConfig initialisation, config() function |
packages/zod/src/v4/classic/tests/global-config.test.ts | Tests verifying singleton identity, write visibility, and pre-import bootstrap |
| PR #5889 | Original rationale and design notes for promoting config to globalThis |
| Error Customization docs | Full error-map API and precedence reference |