Skip to content

MiddlewareRequest on ntl dev throws error: MiddlewareRequest only works in a Netlify Edge Function environment #2735

Open
@ghost

Description

The following middleware.ts in a newly created next app using npx create-next-app@latest, running with netlify dev, throws an error:

import { MiddlewareRequest, NextRequest } from '@netlify/next'

export async function middleware(req: NextRequest) {

  const request = new MiddlewareRequest(req)
  
  return request.next()
}

The error:

Error: MiddlewareRequest only works in a Netlify Edge Function environment
    at middleware (middleware.ts:7:18)
   5 | export default async function middleware(req: NextRequest) {
   6 |
>  7 |   const request = new MiddlewareRequest(req)
     |                  ^
   8 |   
   9 |   return request.next()
  10 | }

I'm aware this is a copy of #2003, but the solution given -- use netlify dev instead of next dev -- does not seem to work (anymore).

Maybe something to do with new both React and Next versions with major upgrades?

This feature is important to me, it determines whether to switch from Vercel or not...

EDIT: The same error occurs when running a build. The build itself completes without errors, but the app throws a 500 with the same error message as above.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions