Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Middleware Throws Error with Lazy Init since 5.0.0-beta.20 #12267

Open
MoscoviumAlchemist opened this issue Nov 20, 2024 · 0 comments
Open

Middleware Throws Error with Lazy Init since 5.0.0-beta.20 #12267

MoscoviumAlchemist opened this issue Nov 20, 2024 · 0 comments
Labels
bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.

Comments

@MoscoviumAlchemist
Copy link

MoscoviumAlchemist commented Nov 20, 2024

Environment

  Binaries:
    Node: 20.16.0 - ~/.nvm/versions/node/v20.16.0/bin/node
    Yarn: 1.22.19 - ~/.yarn/bin/yarn
    npm: 10.8.1 - ~/.nvm/versions/node/v20.16.0/bin/npm
    pnpm: 9.12.0 - /opt/homebrew/bin/pnpm
  npmPackages:
    @auth/core: ^0.37.4 => 0.37.4 
    next: 15.0.3 => 15.0.3 
    next-auth: ^5.0.0-beta.25 => 5.0.0-beta.25 
    react: 19.0.0-rc-69d4b800-20241021 => 19.0.0-rc-69d4b800-20241021 

Reproduction URL

https://github.com/MoscoviumAlchemist/nextjs-authjs-lazy-init-with-middleware/tree/lazy-init-middleware-bug

Describe the issue

⨯ Error: The Middleware "/middleware" must export a `middleware` or a `default` function at [project]/node_modules/next/dist/esm/build/templates/middleware.js { INNER_MIDDLEWARE_MODULE => "[project]/src/middleware.ts [middleware] (ecmascript)" } [middleware] (ecmascript) (.next/server/edge/chunks/_608f43._.js:11284:11) at <unknown> (.next/server/edge/chunks/edge-wrapper_8e4124.js:693:27) at runModuleExecutionHooks (.next/server/edge/chunks/edge-wrapper_8e4124.js:738:9) at instantiateModule (.next/server/edge/chunks/edge-wrapper_8e4124.js:691:9) at getOrInstantiateModuleFromParent (.next/server/edge/chunks/edge-wrapper_8e4124.js:624:12) at esmImport (.next/server/edge/chunks/edge-wrapper_8e4124.js:142:20) at <unknown> (.next/server/edge/chunks/_608f43._.js:11323:69)

How to reproduce

A) Clone the repository https://github.com/MoscoviumAlchemist/nextjs-authjs-lazy-init-with-middleware/tree/lazy-init-middleware-bug
B) Add .env or .env.local file with:

AUTH_TRUST_HOST=true
AUTH_SECRET= # Linux: `openssl rand -hex 32` or go to https://generate-secret.vercel.app/32
DEBUG_AUTH_SESSIONS=false

C) Run the app by doing:
npm run dev

Downgrading to 5.0.0-beta.19 the middleware won't throw errors, but since 5.0.0-beta.20 it throws

`Error: The Middleware "/middleware" must export a `middleware` or a `default` function`

Removing lazy init (by editing auth.ts to remove the request param) and the middleware also loads correctly.

Expected behavior

Middleware should export as nextjs expects even when next-auth lazy init is enabled.

@MoscoviumAlchemist MoscoviumAlchemist added bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime. labels Nov 20, 2024
@MoscoviumAlchemist MoscoviumAlchemist changed the title Middleware Throws Error with Lazy Init since v20 Middleware Throws Error with Lazy Init since 5.0.0-beta.20 Nov 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.
Projects
None yet
Development

No branches or pull requests

1 participant