nextjs - ✅(Solved) Fix Next does not tree-shake Zod correctly [1 pull requests, 2 comments, 3 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
vercel/next.js#88643Fetched 2026-04-08 02:04:12
View on GitHub
Comments
2
Participants
3
Timeline
19
Reactions
17
Author
Timeline (top)
subscribed ×8labeled ×3commented ×2cross-referenced ×2

Root Cause

I did this because ./mini in the package.json links to zod/mini/index.js, which just imports zod/v4/mini/index.js, which just imports zod/v4/mini/externals.js.

Fix Action

Fix / Workaround

A few workarounds to make it work:

PR fix notes

PR #88878: Turbopack: Tree-shake auto-detected barrel files

Description (problem / solution / changelog)

When following re-export chains, Turbopack would stop and include the entire module if it isn't marked sideEffects: false in package.json. This causes barrel files to pull in all their dependencies even when only one export is used.

As the existing comment notes, it is unfortunate that we have to use the whole module here, but we can fix that by allowing modules marked ModuleEvaluationIsSideEffectFree to be followed through in re-export chains. This fixes tree-shaking for packages like zod/mini where intermediate barrel files don't explicitly set sideEffects: false.

Fixes: #88643

Changed files

  • turbopack/crates/turbopack-ecmascript/src/references/esm/export.rs (modified, +1/-5)
  • turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs (modified, +1/-1)
  • turbopack/crates/turbopack-tests/tests/execution/turbopack/side-effects-optimization/reexport-barrel-no-directive/input/index.js (added, +15/-0)
  • turbopack/crates/turbopack-tests/tests/execution/turbopack/side-effects-optimization/reexport-barrel-no-directive/input/lib/bar.js (added, +1/-0)
  • turbopack/crates/turbopack-tests/tests/execution/turbopack/side-effects-optimization/reexport-barrel-no-directive/input/lib/foo.js (added, +1/-0)
  • turbopack/crates/turbopack-tests/tests/execution/turbopack/side-effects-optimization/reexport-barrel-no-directive/input/lib/index.js (added, +6/-0)
  • turbopack/crates/turbopack-tests/tests/execution/turbopack/side-effects-optimization/reexport-barrel-no-directive/options.json (added, +3/-0)

Code Example

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 25.2.0: Tue Nov 18 21:09:40 PST 2025; root:xnu-12377.61.12~1/RELEASE_ARM64_T6000
  Available memory (MB): 32768
  Available CPU cores: 10
Binaries:
  Node: 24.11.1
  npm: 11.6.2
  Yarn: N/A
  pnpm: 10.15.0
Relevant Packages:
  next: 16.1.2 // Latest available version is detected (16.1.2).
  eslint-config-next: N/A
  react: 19.2.3
  react-dom: 19.2.3
  typescript: 5.9.3
Next.js Config:
  output: N/A

---

npm run build && npx next experimental-analyze

---

"./mini": {
  "@zod/source": "./src/mini/index.ts",
  "types": "./mini/index.d.cts",
  "import": "./v4/mini/external.js",   <-- only line changed
  "require": "./mini/index.cjs"
},
RAW_BUFFERClick to expand / collapse

Link to the code that reproduces this issue

https://github.com/DoroGi/nextjs-zod-tree-shaking-issues/tree/main

I created this repo running: npx create-next-app@latest my-app --yes as suggested by NextJS "getting started".

Then, in a second commit, I installed Zod and created a simple client component called Zodder. This component simply imports Zod using import * as z from zod/mini and parses a string.

To Reproduce

Current vs. Expected behavior

You will see that the bundle contains all Zod locales, which are unused and very heavy; I expect they should be tree-shaken away.

I suspect this tree-shaking issue applies to many other packages where the impact is less obvious.

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 25.2.0: Tue Nov 18 21:09:40 PST 2025; root:xnu-12377.61.12~1/RELEASE_ARM64_T6000
  Available memory (MB): 32768
  Available CPU cores: 10
Binaries:
  Node: 24.11.1
  npm: 11.6.2
  Yarn: N/A
  pnpm: 10.15.0
Relevant Packages:
  next: 16.1.2 // Latest available version is detected (16.1.2).
  eslint-config-next: N/A
  react: 19.2.3
  react-dom: 19.2.3
  typescript: 5.9.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Module Resolution

Which stage(s) are affected? (Select all that apply)

next build (local)

Additional context

A few workarounds to make it work:

To try this methods, remember to run each time the line below, so you update the .next and you can check whether the locales are present in the bundle or not:

npm run build && npx next experimental-analyze
  1. Importing zod instead of zod/mini in the zodder component seems to work. Locales are not present anymore.
  2. Going into node_modules/zod/package.json and editing the ./mini exports configuration to this seems to work:
"./mini": {
  "@zod/source": "./src/mini/index.ts",
  "types": "./mini/index.d.cts",
  "import": "./v4/mini/external.js",   <-- only line changed
  "require": "./mini/index.cjs"
},

I did this because ./mini in the package.json links to zod/mini/index.js, which just imports zod/v4/mini/index.js, which just imports zod/v4/mini/externals.js.

  1. Removing the line export { z }; in zod/v4/mini/index.js seems to work. Notice that the import line when using the standard import * as z from 'zod' (which is just zod/index.js -> ./v4/classic/external.js) has a similar line in zod/index.js, but it's not causing any issue apparently.

Related issues

This seems to have been talked a lot in the zod issues. Everybody seems to have found their solution, the only remaining ones are the issues related to NextJS

https://github.com/colinhacks/zod/issues/4433 https://github.com/colinhacks/zod/issues/4572 https://github.com/colinhacks/zod/issues/4637 https://github.com/colinhacks/zod/issues/4798 https://github.com/colinhacks/zod/issues/5206 https://github.com/colinhacks/zod/issues/5561

I also created an identical issue in Zod: https://github.com/colinhacks/zod/issues/5641

extent analysis

TL;DR

Importing zod instead of zod/mini in the component seems to be a viable workaround to prevent unnecessary locales from being included in the bundle.

Guidance

  • Try importing zod directly instead of zod/mini to see if it resolves the issue.
  • Verify the fix by running npm run build && npx next experimental-analyze and checking if the locales are still present in the bundle.
  • If the issue persists, consider editing the ./mini exports configuration in node_modules/zod/package.json as a temporary workaround.
  • Removing the export { z }; line in zod/v4/mini/index.js may also resolve the issue, but this might have unintended consequences and should be approached with caution.

Example

No code snippet is provided as the issue is related to module resolution and tree-shaking, and the provided workarounds are sufficient for guidance.

Notes

The root cause of the issue seems to be related to how zod/mini is exported and imported, leading to unnecessary locales being included in the bundle. The provided workarounds may not be ideal solutions but can help mitigate the issue until a more permanent fix is found.

Recommendation

Apply the workaround of importing zod instead of zod/mini as it seems to be the simplest and most straightforward solution. This approach may help prevent unnecessary locales from being included in the bundle, but it's essential to monitor the issue and adjust the solution as needed.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

nextjs - ✅(Solved) Fix Next does not tree-shake Zod correctly [1 pull requests, 2 comments, 3 participants]