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

Next.js 14.1.4中NextAuth报错:Cannot read properties of undefined (reading 'GET')

解决Next.js 14.1.4 + NextAuth 登录页TypeError问题

问题诊断

你遇到的TypeError: Cannot read properties of undefined (reading 'GET')错误,核心原因是NextAuth的导入路径错误,或路由文件配置存在问题,导致handlers对象未被正确初始化。

修复步骤

1. 修正Auth文件的导入路径

你的src/auth.ts中导入NextAuth的路径错误,NextAuth v5(适配Next.js 13+ App Router)的正确入口是next-auth,而非next-auth/next。修改代码如下:

import NextAuth from "next-auth"; // 修正此处的导入路径
import GoogleProvider from "next-auth/providers/google";

export const {
  handlers: { GET, POST },
  auth,
  signIn,
  signOut,
} = NextAuth({
  providers: [
    GoogleProvider({
      clientId: process.env.GOOGLE_CLIENT_ID as string,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
    }),
  ],
});

2. 检查路由文件路径与命名

确保你的路由文件路径完全正确:

  • 文件夹名应为[...nextauth](注意不要有多余的反斜杠)
  • 完整路径:src/app/api/auth/[...nextauth]/route.ts
  • 路由文件代码保持不变:
export { GET, POST } from "@/auth";

3. 验证环境变量与依赖版本

  • 在项目根目录的.env.local文件中,确认已正确配置GOOGLE_CLIENT_ID和GOOGLE_CLIENT_SECRET,且无拼写错误
  • 确认安装的是NextAuth v5(适配App Router),可通过查看package.json或执行npm install next-auth@beta更新版本
  • 重启Next.js开发服务器,确保环境变量生效

4. 测试登录页面

完成上述修改后,重新访问http://localhost:3000/api/auth/signin,应该能正常加载Google登录界面。

内容的提问来源于stack exchange,提问作者Atticus

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 02:57:43