Installation
System Requirements
- Node.js: ≥ 20
- TypeScript: ≥ 5.0 (recommended)
- Zod: v3.x or v4.x
Package Selection
zod-codepen provides multiple packages for different needs:
Installation
Runtime Serialization
bash
# Zod v3
npm install @zod-codepen/zod-v3
# Zod v4
npm install @zod-codepen/zod-v4bash
# Zod v3
pnpm add @zod-codepen/zod-v3
# Zod v4
pnpm add @zod-codepen/zod-v4bash
# Zod v3
yarn add @zod-codepen/zod-v3
# Zod v4
yarn add @zod-codepen/zod-v4Vite Plugin (Build-Time Transform)
If you use Vite, you can install the plugin to automatically transform schemas at build time:
bash
npm install -D @zod-codepen/vite-pluginbash
pnpm add -D @zod-codepen/vite-pluginbash
yarn add -D @zod-codepen/vite-pluginConfigure vite.config.ts:
typescript
import { defineConfig } from 'vite';
import zodCodepen from '@zod-codepen/vite-plugin';
export default defineConfig({
plugins: [zodCodepen()]
});Peer Dependencies
Each package requires the corresponding version of Zod as a peer dependency. If you don't have Zod installed yet, install it together:
bash
# Zod v3
npm install zod@3 @zod-codepen/zod-v3
# Zod v4
npm install zod@4 @zod-codepen/zod-v4bash
# Zod v3
pnpm add zod@3 @zod-codepen/zod-v3
# Zod v4
pnpm add zod@4 @zod-codepen/zod-v4bash
# Zod v3
yarn add zod@3 @zod-codepen/zod-v3
# Zod v4
yarn add zod@4 @zod-codepen/zod-v4TypeScript Configuration
zod-codepen is written in TypeScript and provides complete type definitions. Recommended tsconfig.json:
json
{
"compilerOptions": {
"target": "ES2022",
"module": "ESNext",
"moduleResolution": "bundler",
"strict": true,
"esModuleInterop": true
}
}ESM Modules
zod-codepen provides ESM module format only. Make sure your project supports ESM:
json
// package.json
{
"type": "module"
}Or use the .mjs extension:
javascript
// script.mjs
import { serialize } from '@zod-codepen/zod-v3';Verify Installation
Create a test file to verify the installation was successful:
typescript
// test.ts
import { serialize } from '@zod-codepen/zod-v3'; // or zod-v4
import { z } from 'zod';
const schema = z.object({
name: z.string(),
age: z.number(),
});
console.log(serialize(schema));
// Should output:
// z.object({
// name: z.string(),
// age: z.number()
// })Run the test:
bash
npx tsx test.tsTroubleshooting
Module Resolution Error
If you encounter a Cannot find module error, make sure:
- Using
"moduleResolution": "bundler"or"node16" - Project is configured for ESM modules
Type Errors
If TypeScript reports type errors, make sure:
- TypeScript version ≥ 5.0
- The correct version of Zod is installed
@types/nodeis installed (if needed)
Zod Version Mismatch
If you encounter peer dependency warnings:
bash
# Check Zod version
npm ls zod
# Install the correct version
npm install zod@3 # or zod@4bash
# Check Zod version
pnpm ls zod
# Install the correct version
pnpm add zod@3 # or zod@4bash
# Check Zod version
yarn list zod
# Install the correct version
yarn add zod@3 # or zod@4