You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Next.js 13.5.5 + next-auth 4.23生产构建报错求助

解决Next.js 13.5.5 + next-auth 4.23生产构建Edge Runtime报错问题

问题重现

Failed to compile.

./node_modules/@babel/runtime/regenerator/index.js
Dynamic Code Evaluation (e. g. 'eval', 'new Function', 'WebAssembly.compile') not allowed in Edge Runtime

Import trace for requested module:
./node_modules/@babel/runtime/regenerator/index.js
./node_modules/next-auth/react/index.js

Build 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 10:29:53