Installing and Importing Zod
What you'll learn: How to add Zod to your project and bring its schema builders into your code.
Why Zod?
You've learned how JSDoc and @ts-check help catch type errors before runtime. But what about validating data that arrives during runtime—like API responses, user input, or environment variables? That's where Zod shines. It's a TypeScript-first schema validation library that works perfectly in plain JavaScript too.
Think of Zod as a bouncer at a club: it checks incoming data against your rules and only lets valid data through. Unlike JSDoc (which only helps during development), Zod validates at runtime, protecting your app from unexpected data shapes.
Installing Zod
Add Zod to your project using your preferred package manager:
npm install zod
Or with Yarn:
yarn add zod
Or with pnpm:
pnpm add zod
Zod should go in your regular dependencies (not devDependencies) because your production code will use it to validate data at runtime.
Importing Zod
Once installed, import the parts you need. The most common import brings in the schema builder z:
import { z } from 'zod';
If you're using CommonJS:
const { z } = require('zod');
The z object is your toolkit—it contains methods like z.string(), z.number(), and z.object() that you'll use to define validation schemas.
What's Next?
With Zod installed and imported, you're ready to start building schemas that describe what valid data looks like. You'll define rules, parse incoming data, and catch problems before they cause bugs.
Key Takeaway: Install Zod as a regular dependency and import the z builder to start defining runtime validation schemas that protect your application from invalid data.