nextauthjs / next-auth

Authentication for the Web.
https://authjs.dev
ISC License
24.09k stars 3.33k forks source link

NextAuth v5 `TypeError: next_dist_server_web_exports_next_request__WEBPACK_IMPORTED_MODULE_0__ is not a constructor` #9922

Closed chungweileong94 closed 7 months ago

chungweileong94 commented 7 months ago

Environment

System:
    OS: macOS 14.3
    CPU: (8) arm64 Apple M2
    Memory: 2.59 GB / 16.00 GB
    Shell: 5.9 - /bin/zsh
Binaries:
    Node: 18.19.0 - ~/Library/Caches/fnm_multishells/18527_1707202116497/bin/node
    Yarn: 1.22.21 - ~/Library/Caches/fnm_multishells/18527_1707202116497/bin/yarn
    npm: 10.2.3 - ~/Library/Caches/fnm_multishells/18527_1707202116497/bin/npm
    pnpm: 8.14.1 - ~/Library/Caches/fnm_multishells/18527_1707202116497/bin/pnpm
    bun: 1.0.26 - /opt/homebrew/bin/bun
Browsers:
    Safari: 17.3
npmPackages:
    next: 14.0.4 => 14.0.4 
    next-auth: 5.0.0-beta.8 => 5.0.0-beta.8 
    react: ^18 => 18.2.0

Reproduction URL

https://github.com/chungweileong94/nextauth-route-handler-error

Describe the issue

If one of the AUTH_URL and NEXTAUTH_URL is set, we will get the following error.

TypeError: next_dist_server_web_exports_next_request__WEBPACK_IMPORTED_MODULE_0__ is not a constructor

How to reproduce

  1. Clone the repro
  2. Run the app via pnpm dev
  3. Navigate to http://localhost:3000/api/auth/signin

Expected behavior

It should show the next-auth built-in sign-in page. Instead, it shows throw 500 error (TypeError: next_dist_server_web_exports_next_request__WEBPACK_IMPORTED_MODULE_0__ is not a constructor)

chungweileong94 commented 7 months ago

The problem seems to be caused by new NextRequest(...) in reqWithEnvURL https://github.com/nextauthjs/next-auth/blob/8a2ca6b280457f202ff77f74dc699cbf85606d43/packages/next-auth/src/lib/env.ts#L11

Which is why setting either AUTH_URL or NEXTAUTH_URL will bypass the problem.

Talent30 commented 7 months ago

I can confirm this bug, same setup

balazsorban44 commented 7 months ago

Related to #9795. This sounds like an upstream Next.js bug which I thought has been fixed. Will have a look there. The workaround is to patch new NextRequest to new NextRequest.default for now, or drop the env variables.

Note: You most likely don't need AUTH_URL or NEXTAUTH_URL anyway, it's not been required for a very long time, unless you want a custom path for your auth route, or behind a proxy.

chungweileong94 commented 7 months ago

Note: You most likely don't need AUTH_URL or NEXTAUTH_URL, it's not been required for a very long time, unless you want a custom path for your auth route, or behind a proxy.

Ah, good to know.

balazsorban44 commented 7 months ago

Fix is on the way in Next.js https://github.com/vercel/next.js/pull/61721

chungweileong94 commented 7 months ago

Will close this issue, as it has been fixed in the latest NextJS canary release.

Zelzazor commented 7 months ago

Note: You most likely don't need AUTH_URL or NEXTAUTH_URL anyway, it's not been required for a very long time, unless you want a custom path for your auth route, or behind a proxy.

Thing is, I do use a proxy because I am working with a Docker container and using nginx to deploy the app. What are my options in this case?

EDIT: Nevermind my message, I was able to make the auth work using NEXTAUTH_URL_INTERNAL instead.

itskaransingh commented 7 months ago

Hi @Zelzazor , can you please tell me how you were able to make the auth work using NEXTAUTH_URL_INTERNAL, thanks.

irclausen commented 6 months ago

Hi @Zelzazor , can you please tell me how you were able to make the auth work using NEXTAUTH_URL_INTERNAL, thanks.

I was able to apply a fix for local dev at least by changing:

AUTH_URL=***********

to

NEXTAUTH_URL_INTERNAL=***********

in my .env file. If you don't have AUTH_URL perhaps add NEXTAUTH_URL_INTERNAL instead of replacing

itskaransingh commented 6 months ago

Thanks, btw my error resolved when i shifted to previous version 5.0.0-beta.4 from 5.0.0-beta.8

bwheeele commented 6 months ago

I am still getting this error. I tried adding AUTH_URL , NEXTAUTH_URL_INTERNAL and still keep getting this error.

chungweileong94 commented 6 months ago

@bwheeele This is an upstream Next.js bug, which has been fixed in https://github.com/vercel/next.js/pull/61721, and landed in the latest Next.js canary version, but yet to be landed in the stable version.

paxbowlski commented 6 months ago

@chungweileong94 just installed 14.1.1-canary.73 and it resolved the issue for me. Thank you!

oscarmylla commented 6 months ago

I've encountered a potential edge case when using the prisma adapter together with prisma accelerate and the following versions in my project:

@prisma/client: ^5.10.2 @prisma/extension-accelerate: ^0.6.3 next-auth: ^5.0.0-beta.13 next: ^14.1.0,

I observed that this issue was resolved when I set the route handler to run on the edge.


This doesn't work:

import { auth } from "@/server/auth";

export const GET = auth(async (req) => {
   const session = req.auth
   ...
   return Response.json({data})
})

This works:

import { auth } from "@/server/auth";

export const runtime = 'edge';

export const GET = auth(async (req) => {
   const session = req.auth
   ...
   return Response.json({data})
})
philippemiguet commented 4 months ago

Hi @Zelzazor , can you please tell me how you were able to make the auth work using NEXTAUTH_URL_INTERNAL, thanks.

I was able to apply a fix for local dev at least by changing:

AUTH_URL=***********

to

NEXTAUTH_URL_INTERNAL=***********

in my .env file. If you don't have AUTH_URL perhaps add NEXTAUTH_URL_INTERNAL instead of replacing

This is what fixed it for me on "next-auth": "^5.0.0-beta.17"

IhorAnDev commented 4 months ago

This is what fixed it for me on "next-auth": "^5.0.0-beta.17"

God, bless you! Thanks a million.