Error Invalid input: expected nonoptional, received undefined → at children
After spending hours debugging this issue, I found this PR #4990 that helped me understand and fix the root cause. However, even after the fix was merged, the issue still seems to persist in some environments.
Issue
This error appears when using:
[email protected][email protected][email protected]- Next.js App Router (especially with MDX routes)
Error:
This issue is still reproducible in some environments even with:
Root cause
Nextra's Layout component validates props using Zod:
LayoutPropsSchema.safeParse(themeConfig)However, children is destructured before validation:
({ children, ...themeConfig } = props)So children is NOT included in the object passed to Zod.
With Zod 4.4.x, missing required keys now throw BEFORE custom validation runs, resulting in:
✖ Invalid input: expected nonoptional, received undefined → at childrenThis is caused by stricter validation behavior in Zod 4.4 combined with Nextra's prop handling.
Temporary workaround (manual fix)
Until an official fix is fully available, the schema input must include children.
File location:
node_modules/nextra-theme-docs/dist/layout.jsChange:
LayoutPropsSchema.safeParse(themeConfig)To:
LayoutPropsSchema.safeParse({ children, ...themeConfig })Persistent fix (recommended)
For pnpm projects, you can persist this fix using the patch workflow:
pnpm add -D patch-package
pnpm patch nextra-theme-docsThen edit
dist/layout.jsApply the same fix:
safeParse({ children, ...themeConfig })Finally commit the patch:
pnpm patch-commitThis ensures the fix survives reinstallations and CI/CD environments.
Source: shuding/nextra