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

Next.js边缘运行时不支持Node.js stream模块错误排查

问题

我在本地用Docker Compose搭配MongoDB运行Next.js,正在配置认证(Auth)和中间件(middleware)。middleware.ts通过const { auth } = NextAuth(authConfig);引用auth.config.ts,其中Credentials认证的authorize函数代码如下:

import bcrypt from "bcryptjs";
import type { NextAuthConfig } from "next-auth";
import Credentials from "next-auth/providers/credentials";
import Github from "next-auth/providers/github";
import Google from "next-auth/providers/google";

import { LoginSchema } from "@/models";
import { getUserByEmail } from "@/data/user";

export default {
 providers: [
  Google(...),
  Github(...),
  Credentials({
   async authorize(credentials) {
        const validatedFields = LoginSchema.safeParse(credentials);

        if (validatedFields.success) {
          const { email, password } = validatedFields.data;
          
          const user = await getUserByEmail(email);
          if (!user || !user.password) return null;

          const passwordsMatch = await bcrypt.compare(
            password,
            user.password,
          );

          if (passwordsMatch) return user;
        }

        return null;
      }
  })
 ]
}

现在遇到堆栈错误:

Server Error
Error: The edge runtime does not support Node.js 'stream' module.
Learn More: https://nextjs.org/docs/messages/node-module-in-edge-runtime

This error happened while generating the page. Any console logs will be displayed in the terminal window.
Call Stack
<unknown>
webpack-internal:///(middleware)/./node_modules/next/dist/esm/server/web/globals.js (33)
Object.get
webpack-internal:///(middleware)/./node_modules/next/dist/esm/server/web/globals.js (33:19)
eval
webpack-internal:///(middleware)/./node_modules/mongodb/lib/cursor/abstract_cursor.js (617:45)
(middleware)/./node_modules/mongodb/lib/cursor/abstract_cursor.js
file:///home/loki/projects/nextjs/fire-project/fire-dashboard/.next/server/src/middleware.js (864:1)
__webpack_require__
file:///home/loki/projects/nextjs/fire-project/fire-dashboard/.next/server/edge-runtime-webpack.js (37:33)
fn
file:///home/loki/projects/nextjs/fire-project/fire-dashboard/.next/server/edge-runtime-webpack.js (280:21)
eval
webpack-internal:///(middleware)/./node_modules/mongodb/lib/cursor/aggregation_cursor.js (9:27)
(middleware)/./node_modules/mongodb/lib/cursor/aggregation_cursor.js
file:///home/loki/projects/nextjs/fire-project/fire-dashboard/.next/server/src/middleware.js (875:1)
__webpack_require__
file:///home/loki/projects/nextjs/fire-project/fire-dashboard/.next/server/edge-runtime-webpack.js (37:33)
fn
file:///home/loki/projects/nextjs/fire-project/fire-dashboard/.next/server/edge-runtime-webpack.js (280:21)
eval
webpack-internal:///(middleware)/./node_modules/mongodb/lib/collection.js (10:30)

我特意用了bcryptjs避免bcrypt依赖stream模块,但注释掉authorize函数后错误就消失了。想问:

  1. 哪里操作有误?
  2. 部署到Docker是否需要用edge runtime?该怎么修改运行时配置?

解答

错误原因

问题出在Next.js中间件默认使用Edge Runtime,而你的getUserByEmail调用了MongoDB官方驱动,该驱动依赖Node.js的stream模块——Edge Runtime不支持这个模块。注释掉authorize函数后,不会触发数据库查询,所以错误消失。

解决办法

方案1:强制中间件使用Node.js Runtime

在middleware.ts文件顶部添加运行时配置,让中间件跑在Node.js环境下:

export const config = {
  runtime: 'nodejs',
};

这样就能兼容MongoDB驱动的所有依赖。

方案2:分离认证逻辑到API路由

NextAuth.js的Credentials认证默认会在中间件流程中执行,你可以把认证逻辑移到独立的API路由中,或者配置NextAuth使用自定义登录页,让数据库查询在Node.js Runtime的API路由里完成,避开Edge环境的限制。

Docker部署与运行时配置

部署到Docker不需要强制使用Edge Runtime,完全可以用默认的Node.js Runtime。如果需要切换运行时:

  • App Router场景:在需要修改的路由文件或中间件文件顶部添加配置:
    export const config = {
      runtime: 'nodejs', // 或 'edge'
    };
    
  • Pages Router场景:在next.config.js中全局配置(不推荐全局修改,建议针对单个文件配置):
    module.exports = {
      experimental: {
        runtime: 'nodejs', // 或 'edge'
      },
    };
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 12:45:31