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

执行next build时因[...nextauth]/route.js中出现TypeError: o is not a function错误导致构建失败

执行next build时因[...nextauth]/route.js中出现TypeError: o is not a function错误导致构建失败

我之前也踩过一模一样的坑!当时跑next build准备生产包,卡在收集页面数据那步半天不动,最后报这个错,折腾好久才搞定。其实这个o是构建压缩时被混淆后的变量名,本质是你的NextAuth配置或者导入环节出了小问题,给你几个排查方向:

  • 先核对NextAuth的导入是否正确
    一定要在app/api/auth/[...nextauth]/route.js里用对导入方式,别把默认导入和命名导入搞混,正确写法应该是这样的:

    import NextAuth from "next-auth"
    import CredentialsProvider from "next-auth/providers/credentials"
    

    要是你错写成import { CredentialsProvider } from "next-auth"(把命名导入和默认导入搞反),或者路径写错了,构建压缩后就会出现这种“变量不是函数”的报错。

  • 检查CredentialsProvider的配置代码
    你代码里的o对应的就是CredentialsProvider这个函数,要是你没正确导入就直接用,或者配置时拼写错了函数名、漏写了调用括号,都会触发这个错误。给你个正确的配置模板参考:

    export const authOptions = {
      providers: [
        CredentialsProvider({
          name: "Credentials",
          credentials: {
            email: { label: "邮箱", type: "email" },
            password: { label: "密码", type: "password" }
          },
          async authorize(credentials) {
            // 这里放你的用户授权逻辑,比如查询数据库验证账号密码
          }
        })
      ]
    }
    
    const handler = NextAuth(authOptions)
    export { handler as GET, handler as POST }
    

    重点确认CredentialsProvider是作为函数被正确调用的,参数结构也别写错。

  • 验证版本兼容性
    如果你用的是Next.js 13+的App Router模式,必须搭配next-auth@4.20.0及以上的版本,版本不匹配也会出现这种奇怪的构建错误。可以更新下依赖再试:

    npm update next-auth
    # 用yarn的话就跑 yarn upgrade next-auth
    
  • 清理缓存后重新构建
    有时候旧的构建缓存会残留错误代码,先删掉.next文件夹和缓存目录,再重新执行构建:

    rm -rf .next node_modules/.cache
    npm run build
    

按上面的步骤排查一遍,基本就能解决这个构建卡住的问题啦!

备注:内容来源于stack exchange,提问作者goodwill

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.13 17:19:35