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

Using next-auth in the middleware causes problems in standalone mode #7300

Closed
meesvandongen opened this issue Apr 18, 2023 · 2 comments
Closed
Labels
triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime. upstream The issue dervies from one of next-auth dependencies

Comments

@meesvandongen
Copy link
Contributor

meesvandongen commented Apr 18, 2023

Environment

next@13.3.0
next-auth@4.22.0

Reproduction URL

https://github.com/meesvandongen/next-auth-repro-standalone-middleware

Describe the issue

Including export { default } from "next-auth/middleware" causes an error in standalone mode:

warn  - Compiled with warnings

./node_modules/next/dist/server/web/spec-extension/image-response.js
A Node.js API is used (process.turbopack at line: 47) which is not supported in the Edge Runtime.
Learn more: https://nextjs.org/docs/api-reference/edge-runtime

Import trace for requested module:
./node_modules/next/dist/server/web/spec-extension/image-response.js
./node_modules/next/server.js
./node_modules/next-auth/next/middleware.js
./node_modules/next-auth/middleware.js

This will in turn lead to these modules not being found by nextjs runtime.

It seems that because of the bundling strategy? ImageResponse is somehow included in the import. Doing so manually

import { NextResponse, ImageResponse } from "next/server"

export default function middleware(req, res) {
  const response = NextResponse.next()
  ImageResponse;
  return response
}

Will cause a similar error.

If we inspect the output of the bundler, we can see the following, which is likely the cause:

var _server = require("next/server");

An option would be to swap babel for tsup or just tsc to produce esm modules.

How to reproduce

Add a middleware.js file

export { default } from "next-auth/middleware"

set next.config.js

/** @type {import('next').NextConfig} */
module.exports = {
  output: 'standalone',
}

run the build

Expected behavior

The modules which are not supported are not included in the build.

@meesvandongen meesvandongen added the triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime. label Apr 18, 2023
@TrevorThomp
Copy link

I had the same issue with Nextjs-auth0 middleware and it looks like this auth as well. I have an open issue in Nextjs for this same error that you can track as well:

vercel/next.js#48302

@balazsorban44 balazsorban44 added the upstream The issue dervies from one of next-auth dependencies label Apr 18, 2023
@balazsorban44
Copy link
Member

Closing in favor of vercel/next.js#48302

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime. upstream The issue dervies from one of next-auth dependencies
Projects
None yet
Development

No branches or pull requests

3 participants