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

Next.js 15.0.3中next-auth的AuthOptions导出及类型报错求助

解决Next.js 15.0.3 + NextAuth.js的TypeScript类型错误

错误信息

Module '"next-auth"' has no exported member 'AuthOptions'. Did you mean to use 'import AuthOptions from "next-auth"' instead?
Binding element 'user' implicitly has an 'any' type.
Binding element 'account' implicitly has an 'any' type.
Binding element 'profile' implicitly has an 'any' type.

问题原因

适配Next.js 15的NextAuth.js多为v5版本,其API与传统v4版本存在差异,AuthOptions的导出方式、回调函数的类型定义逻辑均有变化;同时你的代码存在括号不匹配的语法错误,进一步导致TypeScript无法正确推导参数类型。

修正后的代码

import NextAuth, { type AuthOptions } from "next-auth";
import GoogleProvider from "next-auth/providers/google";
import type { SignInCallbackParams, RedirectCallbackParams } from "next-auth";

export const authOptions: AuthOptions = {
  providers: [
    GoogleProvider({
      clientId: process.env.GOOGLE_CLIENT_ID!,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
    }),
  ],
  callbacks: {
    async signIn({ user, account, profile }: SignInCallbackParams) {
      return true;
    },
    async redirect({ url, baseUrl }: RedirectCallbackParams) {
      return "/details";
    },
  },
};

const handler = NextAuth(authOptions);

export { handler as GET, handler as POST };

关键修正点

  • 类型导入调整:v5版本中AuthOptions需从next-auth命名导出获取,同时明确导入回调参数类型SignInCallbackParams、RedirectCallbackParams,解决隐式any报错。
  • 语法错误修复:补全signIn和redirect函数的缺失括号,让TypeScript能正确解析函数结构。
  • 环境变量断言:添加!断言环境变量已定义,避免TypeScript关于可选值的报错(若需更严谨,可额外添加环境变量校验逻辑)。

额外排查步骤

如果导入AuthOptions仍提示不存在,先通过package.json确认next-auth为v5版本。版本无误的话,尝试清理依赖缓存后重新安装:

rm -rf node_modules .next package-lock.json
npm install

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 15:03:26