NextJS 14使用Auth.js邮箱认证时遇Edge Runtime不支持stream模块错误
问题:NextJS 14 + Auth.js邮箱认证报错「The edge runtime does not support Node.js 'stream' module」
我在NextJS 14项目中用Auth.js实现无密码邮箱认证,搭配Prisma作为Postgres的ORM。谷歌登录功能正常,但邮箱认证时触发错误:The edge runtime does not support Node.js 'stream' module。已知Prisma不兼容Edge Runtime,已按官方文档将提供者配置抽离到单独文件,寻求无需更换ORM的解决方案。
相关配置文件如下:
auth.config.ts
import Google from "next-auth/providers/google" import Email from "next-auth/providers/email" import type { NextAuthConfig } from "next-auth" export default { providers: [ Email({ server: { host: process.env.SMTP_HOST, port: Number(process.env.SMTP_PORT), auth: { user: process.env.SMTP_USER, pass: process.env.SMTP_PASSWORD, }, }, from: process.env.EMAIL_FROM, }), Google({ clientId: process.env.GOOGLE_CLIENT_ID, clientSecret: process.env.GOOGLE_CLIENT_SECRET, }), ], } satisfies NextAuthConfig
auth.ts
import NextAuth from "next-auth" import authConfig from "@/auth.config" import { PrismaAdapter } from "@auth/prisma-adapter" import { db } from "@/lib/db" export const { handlers: { GET, POST }, auth, } = NextAuth({ adapter: PrismaAdapter(db), session: { strategy: "jwt" }, ...authConfig, })
app/api/auth/[...nextauth]/route.ts
export { GET, POST } from "@/auth"
解决方案
问题根源是NextJS 14 App Router的API路由默认启用Edge Runtime,但Auth.js的Email提供者依赖Node.js核心模块stream,同时Prisma也不支持Edge环境。只需显式指定该路由使用Node.js Runtime即可解决:
修改app/api/auth/[...nextauth]/route.ts,添加runtime配置:
export { GET, POST } from "@/auth" export const runtime = 'nodejs' // 强制使用Node.js Runtime
说明:
- 该配置会让Auth.js的所有认证逻辑运行在Node.js环境下,既兼容Prisma的ORM操作,也能让Email提供者正常调用SMTP服务(依赖
stream模块)。 - 你的现有配置(抽离提供者配置、JWT会话策略)无需改动,保持原样即可。
内容的提问来源于stack exchange,提问作者Ubiquitous Ubuntu
相关产品推荐
相关产品推荐

