zod icon indicating copy to clipboard operation
zod copied to clipboard

v4: use in nextjs middleware generates DynamicCodeEvaluationWarning

Open McCulloughRT opened this issue 8 months ago • 4 comments

Using v4 in nextjs middleware triggers the DynamicCodeEvaluationWarning

Top of the stack trace may be referencing this function, which does attempt new Function("") but catches its expected error (but not the warning): https://github.com/colinhacks/zod/blob/46e96c77af3a72fc4b76558654fcd3f3f8c52b8e/packages/core/src/util.ts#L310

Stack trace: ⚠ DynamicCodeEvaluationWarning: Dynamic Code Evaluation (e. g. 'eval', 'new Function') not allowed in Edge Runtime Learn More: https://nextjs.org/docs/messages/edge-dynamic-code-evaluation at eval (webpack-internal:///(middleware)/../../node_modules/@zod/core/dist/esm/util.js:175:9) at get value (webpack-internal:///(middleware)/../../node_modules/@zod/core/dist/esm/util.js:89:31) at eval (webpack-internal:///(middleware)/../../node_modules/@zod/core/dist/esm/schemas.js:836:74) at $ZodObjectLike.init (webpack-internal:///(middleware)/../../node_modules/@zod/core/dist/esm/core.js:26:13) at eval (webpack-internal:///(middleware)/../../node_modules/@zod/core/dist/esm/schemas.js:946:20) at $ZodObject.init (webpack-internal:///(middleware)/../../node_modules/@zod/core/dist/esm/core.js:26:13) at eval (webpack-internal:///(middleware)/../../node_modules/zod/dist/esm/schemas.js:719:55) at ZodObject.init (webpack-internal:///(middleware)/../../node_modules/@zod/core/dist/esm/core.js:26:13) at new ZodObject (webpack-internal:///(middleware)/../../node_modules/@zod/core/dist/esm/core.js:14:15) at Module.object (webpack-internal:///(middleware)/../../node_modules/zod/dist/esm/schemas.js:749:12) at eval (webpack-internal:///(middleware)/../../packages/db/src/schemas/ProjectDocument/Document/types.ts:18:61) ... REDACTED INTERNAL FILES...

I don't immediately have an idea how to get around triggering the warning, as I'm not sure how next generates it... testing for the runtime env variable before hitting that code is ugly but possible...

McCulloughRT avatar Apr 12 '25 00:04 McCulloughRT

This also generates an unrecoverable error on next build. Looks like zod currently cannot be imported to nextjs middleware (or other libraries that are imported to middleware) at all.

McCulloughRT avatar Apr 12 '25 00:04 McCulloughRT

Any updates on this?

raiyansarker avatar Apr 22 '25 10:04 raiyansarker

How come there is no updates on this?

raiyansarker avatar May 11 '25 20:05 raiyansarker

I just merged a jitless flag that may alleviated this in some cases. https://github.com/colinhacks/zod/pull/4368

But if Next is crashing during the build phase, there's little I can do. That'll require an upstream fix probably. That's not a very reasonable behavior on their part imo.

colinhacks avatar May 15 '25 06:05 colinhacks

Hi, @McCulloughRT. I'm Dosu, and I'm helping the Zod team manage their backlog and am marking this issue as stale.

Issue Summary:

  • You reported that using Zod v4 in Next.js middleware triggers a DynamicCodeEvaluationWarning because Zod uses new Function(""), which is disallowed in the Edge Runtime.
  • This causes build errors, making Zod currently unusable in Next.js middleware without issues.
  • @colinhacks mentioned a recently merged jitless flag that might help in some cases, but build-time crashes in Next.js likely require fixes from Next.js itself.
  • Other users have expressed interest in updates, but no upstream resolution has been confirmed yet.

Next Steps:

  • Please confirm if this issue is still relevant with the latest version of Zod and Next.js; if so, you can keep the discussion open by commenting here.
  • Otherwise, I will automatically close this issue in 7 days.

Thanks for your understanding and contribution!

dosubot[bot] avatar Aug 14 '25 16:08 dosubot[bot]