Strange error when testing with Auth.js v5

Hi there,

I just created a fresh Next.js project and added next-auth version 5 beta for authentication and tRPC for server logic. I have followed the setup documentation for both next-auth and tRPC.

Setup Overview

  • Next.js Version: 14.1.4
  • next-auth Version: 5.0.0-beta.15
### Encountered Issue
During integration tests when testing tRPC procedures that utilize next-auth for session handling, I'm getting this weird error:

Error: Cannot find module '/path/to/project/node_modules/next/server' imported from /path/to/project/node_modules/next-auth/lib/env.js
Did you mean to import next/server.js?

vbnet


This error happens when mocking the tRPC session context through the use of createInnerTRPCContext.

Test Example


Here's the test that triggers the error:

import { createInnerTRPCContext } from "./../server/api/trpc";

test("hello test", async () => {
  const ctx = createInnerTRPCContext({
    session: {
      user: { id: "123", name: "John Doe" },
      expires: "1",
    },
  });
  const caller = createCaller(ctx);

  const res = await caller.test.hello();
});


The error is caused by the createInnerTRPCContext function.

import { auth } from "@/app/api/auth/[...nextauth]/auth";

export const createTRPCContext = async (opts: { headers: Headers }) => {
  const session = await auth();

  return createInnerTRPCContext({ session });
};


Has anyone experienced a similar issue or could provide insights into how to navigate this problem?
Was this page helpful?