Skip to content

NextAuth v5 auth(req, res) fails in pages/ API route #9307

@howard36

Description

@howard36

Environment

$ npx envinfo --system --binaries --browsers --npmPackages "{next,react,next-auth,@auth/*}"
  System:
    OS: macOS 14.0
    CPU: (8) arm64 Apple M2
    Memory: 590.48 MB / 16.00 GB
    Shell: 3.6.1 - /opt/homebrew/bin/fish
  Binaries:
    Node: 18.18.0 - ~/.local/share/nvm/v18.18.0/bin/node
    Yarn: 1.22.19 - ~/.local/share/nvm/v18.18.0/bin/yarn
    npm: 9.8.1 - ~/.local/share/nvm/v18.18.0/bin/npm
    pnpm: 8.11.0 - ~/.local/share/nvm/v18.18.0/bin/pnpm
  Browsers:
    Chrome: 119.0.6045.199
    Safari: 17.0
  npmPackages:
    next: latest => 14.0.3 
    next-auth: 5.0.0-beta.2 => 5.0.0-beta.2 
    react: ^18.2.0 => 18.2.0 

Reproduction URL

https://github.com/howard36/next-auth-v5-example/tree/api-route-bug-repro

Describe the issue

I have a API route at pages/api/endpoint.ts:

import type { NextApiRequest, NextApiResponse } from "next";
import { auth } from "auth";

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse,
) {
  const session = await auth(req, res);
  return res.status(200).json({session});
}

Calling auth(req, res) in this API route causes this error:

 ⨯ Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/Users/howard/git/next-auth-v5-example/node_modules/next/headers' imported from /Users/howard/git/next-auth-v5-example/node_modules/next-auth/lib/index.js
Did you mean to import next-auth-v5-example/node_modules/next/headers.js?
    at new NodeError (node:internal/errors:405:5)
    at finalizeResolution (node:internal/modules/esm/resolve:327:11)
    at moduleResolve (node:internal/modules/esm/resolve:946:10)
    at defaultResolve (node:internal/modules/esm/resolve:1132:11)
    at nextResolve (node:internal/modules/esm/loader:163:28)
    at ESMLoader.resolve (node:internal/modules/esm/loader:835:30)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:424:18)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:77:40)
    at link (node:internal/modules/esm/module_job:76:36) {
  code: 'ERR_MODULE_NOT_FOUND'
}

This is with next-auth 5.0.0-beta.2, next 14.0.3, and node 18.18.0. If I switch to nextjs v13 (specifically v13.5.6), the error changes to:

 ⨯ node_modules/next-auth/lib/index.js (117:0) @ eval
 ⨯ TypeError: Cannot read properties of undefined (reading 'append')
    at eval (webpack-internal:///(api)/./node_modules/next-auth/lib/index.js:128:52)
    at Array.forEach (<anonymous>)
    at cloneSetCookie (webpack-internal:///(api)/./node_modules/next-auth/lib/index.js:128:21)
    at eval (webpack-internal:///(api)/./node_modules/next-auth/lib/index.js:73:13)
    at async handler (webpack-internal:///(api)/./pages/api/endpoint.ts:11:21) {
  page: '/api/endpoint'
}
null

How to reproduce

git clone -b api-route-bug-repro https://github.com/howard36/next-auth-v5-example.git
cd next-auth-v5-example
npm install
cp .env.local.example .env.local
npm run dev

Then visit http://localhost:3000/api/endpoint to trigger the error

Expected behavior

Calling auth(req, res) should successfully return the session without throwing an error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingtriageUnseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions