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

使用Middleware时AuthJS/Next-Auth报TypeError读取modules错误

Next.js Middleware 触发 TypeError: Cannot read properties of undefined (reading 'modules') 错误

正在开发Next.js全栈应用以学习框架核心概念,跟随课程构建项目,技术栈包含NextJS、Prisma、PostgreSQL、AuthJS。执行npm run dev启动开发服务器时,仅在使用Middleware保护部分路由时出现以下错误:
TypeError: Cannot read properties of undefined (reading 'modules')


相关文件内容

package.json

{
  "name": "next-match",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev --turbopack",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },
  "dependencies": {
    "@auth/prisma-adapter": "^2.10.0",
    "@heroui/react": "^2.7.11",
    "@hookform/resolvers": "^5.1.1",
    "@prisma/client": "^6.11.1",
    "bcrypt": "^6.0.0",
    "framer-motion": "^12.19.1",
    "next": "15.3.4",
    "next-auth": "^5.0.0-beta.29",
    "react": "^19.0.0",
    "react-dom": "^19.0.0",
    "react-hook-form": "^7.58.1",
    "react-icons": "^5.5.0",
    "react-toastify": "^11.0.5",
    "zod": "^3.25.67"
  },
  "devDependencies": {
    "@eslint/eslintrc": "^3",
    "@types/bcrypt": "^5.0.2",
    "@types/node": "^20",
    "@types/react": "^19",
    "@types/react-dom": "^19",
    "autoprefixer": "^10.4.21",
    "eslint": "^9",
    "eslint-config-next": "15.3.4",
    "postcss": "^8.5.6",
    "prisma": "^6.10.1",
    "tailwindcss": "^3.4.17",
    "typescript": "^5"
  }
}

auth.ts

import NextAuth from "next-auth";
import authConfig from "@/auth.config";
import { PrismaAdapter } from "@auth/prisma-adapter";
import { prisma } from "./lib/prisma";

export const { handlers, auth, signIn, signOut } = NextAuth({
  callbacks: {
    session: async ({ token, session }) => {
      if (token.sub && session.user) {
        session.user.id = token.sub;
      }
      return session;
    },
  },
  adapter: PrismaAdapter(prisma),
  session: { strategy: "jwt" },
  ...authConfig,
});

auth.config.ts

import Credentials from "next-auth/providers/credentials";
import type { NextAuthConfig } from "next-auth";
import { loginSchema } from "./lib/schemas/login-form-schema";
import { getUserByEmail } from "./app/actions/auth-actions";
import { compare } from "bcrypt";

export default {
  providers: [
    Credentials({
      name: "credentials",
      authorize: async (creds) => {
        const validated = loginSchema.safeParse(creds);

        if (validated.success) {
          const { email, password } = validated.data;
          const user = await getUserByEmail(email);

          if (!user || !(await compare(password, user.password))) return null;

          return user;
        }

        return null;
      },
    }),
  ],
} satisfies NextAuthConfig;

middleware.ts

import { NextResponse } from "next/server";
import { AUTH_ROUTES, PUBLIC_ROUTES } from "./routes";
import { auth } from "./auth";

export default auth((req) => {
  const { nextUrl } = req;
  const isLoggedIn = !!req.auth;

  const isPublic = PUBLIC_ROUTES.includes(nextUrl.pathname);
  const isAuthRoute = AUTH_ROUTES.includes(nextUrl.pathname);

  if (isPublic) {
    return NextResponse.next();
  }

  if (isAuthRoute) {
    if (isLoggedIn) {
      return NextResponse.redirect(new URL("/members", nextUrl));
    }
    return NextResponse.next();
  }

  if (!isPublic && !isLoggedIn) {
    return NextResponse.redirect(new URL("/login", nextUrl));
  }

  return NextResponse.next();
});

export const config = {
  matcher: ["/((?!api|_next/static|_next/image|favicon.ico).*)"],
};

环境信息

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 24.5.0: Tue Apr 22 19:48:46 PDT 2025; root:xnu-11417.121.6~2/RELEASE_ARM64_T8103
  Available memory (MB): 8192
  Available CPU cores: 8
Binaries:
  Node: 22.17.0
  npm: 10.9.2
  Yarn: N/A
  pnpm: 10.12.4
Relevant Packages:
  next: 15.3.4 // 有可用更新版本(15.3.5),建议升级!
  eslint-config-next: 15.3.4
  react: 19.1.0
  react-dom: 19.1.0
  typescript: 5.8.3
Next.js Config:
  output: N/A

已尝试的解决方法

  • 从pnpm切换为npm,匹配课程指定的包管理器
  • 将NextAuth配置拆分为auth.ts和auth.config.ts两个文件
  • 调整@auth/prisma-adapter和next-auth版本以匹配课程使用的版本

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 17:07:13