无法找到模块‘next-auth/adapters’或其类型声明的问题求助
解决NextAuth中
next-auth/adapters模块找不到的问题 问题代码
import NextAuth from "next-auth"; import GoogleProvider from "next-auth/providers/google"; import { PrismaAdapter } from "@auth/prisma-adapter"; import { PrismaClient } from "@prisma/client"; import { NextAuthOptions } from "next-auth"; import { env } from "@/lib/env"; import type { Adapter } from "next-auth/adapters"; const prisma = new PrismaClient(); export const authOptions:NextAuthOptions={ adapter:PrismaAdapter(prisma) as Adapter, providers:[ GoogleProvider({ clientId: env.GOGGLE_CLIENT_ID, clientSecret: env.GOOGLE_SECRET_ID, }), ], }; const handler= NextAuth(authOptions); export {handler as GET, handler as POST}
错误信息
执行import type { Adapter } from "next-auth/adapters";时提示:
Cannot find module 'next-auth/adapters' or its corresponding type declarations
解决方案
- 修正类型导入路径:NextAuth v4.23.0及之后版本,
Adapter类型已移至主模块,将导入语句改为:import type { Adapter } from "next-auth"; - 同步依赖版本:确保
next-auth与@auth/prisma-adapter版本兼容,优先使用同大版本(如v4.x)。重新安装依赖:npm uninstall next-auth @auth/prisma-adapter npm install next-auth @auth/prisma-adapter - 移除冗余类型断言:
PrismaAdapter(prisma)的返回值天然符合Adapter类型,无需额外断言:adapter: PrismaAdapter(prisma), - 检查TS模块解析配置:在
tsconfig.json中确保moduleResolution设置为适配Next.js的模式:{ "compilerOptions": { "moduleResolution": "bundler" // 或 "node16" } }
内容的提问来源于stack exchange,提问作者Krish Chhabra
相关产品推荐
相关产品推荐

