Star import vs named imports #5401
-
|
Hi all, I am not sure if this has been asked before but is there any difference between: import * as z from "zod"as opposed to import { z, ZodError } from "zod"EDIT: This would be for using zod v4 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
Hi @shivan-eyespace! I'm Dosu and I’m helping the zod team. There is a practical difference between star imports ( Some bundlers—especially Next.js with Turbopack—can also throw runtime errors (such as The Zod documentation and maintainers consistently use and recommend star imports for best compatibility and performance docs. If minimizing bundle size is critical, you might also want to look into If this answers your question, feel free to close the issue! To reply, just mention @dosu. How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other |
Beta Was this translation helpful? Give feedback.
Hi @shivan-eyespace! I'm Dosu and I’m helping the zod team.
There is a practical difference between star imports (
import * as z from "zod") and named imports (import { z, ZodError } from "zod") when using Zod. Star imports are generally recommended because they work better with tree-shaking in most bundlers (like Webpack, esbuild, and Next.js), resulting in smaller bundle sizes. Named imports can lead to bundlers including the entire Zod object, making tree-shaking less effective and potentially increasing your bundle size see discussion.Some bundlers—especially Next.js with Turbopack—can also throw runtime errors (such as
ReferenceError: Cannot access 'ZodEnum' before initialization) w…