Next.js 13.5.5 + next-auth 4.23生产构建报错求助
问题重现
Failed to compile.
./node_modules/@babel/runtime/regenerator/index.js
Dynamic Code Evaluation (e. g. 'eval', 'new Function', 'WebAssembly.compile') not allowed in Edge RuntimeImport trace for requested module:
./node_modules/@babel/runtime/regenerator/index.js
./node_modules/next-auth/react/index.jsBuild failed because of webpack errors
Creating an optimized production build
原因分析
Next.js 13的App Router默认启用Edge Runtime,而next-auth/react依赖的@babel/runtime/regenerator包含动态代码评估逻辑(比如eval),这在Edge Runtime环境中是被严格禁止的,直接导致生产构建失败。
解决方案
显式指定组件使用Node.js Runtime:
在所有引入next-auth/react的组件(比如登录组件、布局组件等)顶部添加以下代码,强制组件使用Node.js运行时,避开Edge Runtime的限制:export const runtime = 'nodejs';检查App Router配置:
确认App Router下的layout.js或page.js文件中没有手动设置export const runtime = 'edge';,如果有,要么移除该配置,要么替换为nodejs。升级next-auth版本(可选):
尝试升级到next-auth的最新稳定版本,后续版本可能针对Edge Runtime做了兼容性修复,执行命令:npm install next-auth@latest
内容的提问来源于stack exchange,提问作者Faizan Ahmad

