Next.js Discord

Discord Forum

MIDDLEWARE_INVOCATION_FAILED after upgrading to Node.js v22

Answered
Dogo Guatemalteco posted this in #help-forum
Open in Discord
Dogo GuatemaltecoOP
Support for Node.js v18 is being dropped on Monday in Vercel, so I am trying to upgrade to Node.js v22. After upgrading, it all mostly works fine. However, some pages have stopped working in Vercel with:

500: INTERNAL_SERVER_ERROR
Code: MIDDLEWARE_INVOCATION_FAILED

Looking at the runtime logs, I only get this:

SyntaxError: Unexpected token '<', "<!doctype "... is not valid JSON
    at JSON.parse (<anonymous>)
    at parseJSONFromBytes (node:internal/deps/undici/undici:5738:19)
    at successSteps (node:internal/deps/undici/undici:5719:27)
    at fullyReadBody (node:internal/deps/undici/undici:4609:9)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async consumeBody (node:internal/deps/undici/undici:5728:7)
    at async Ut (user-source.js:40:4123)
    at async Qt (user-source.js:40:5250)
    at async fr (user-source.js:39:25292)
    at async Object.<anonymous> (user-source.js:41:13167)


This error does not occur during development next dev nor does it occur when running local builds next build && next start, so I am not sure how to debug this without being able to reproduce it outside of Vercel.

Any ideas on how to get to the bottom of this?
Answered by Dogo Guatemalteco
I figured out it has nothing to do with Node.js v22.
In order to test the new node version, we deployed new vercel projects instead of modifying existing ones.
All new projects come with "Vercel Authentication" turned on by default in Settings > Deployment Protection. This is somehow interfering with the middleware, so just turning it off fixes the problem. Won't be investigating any further as we do not need the authentication feature currently
View full answer

2 Replies

it's hard to understand you should share middleware code
Dogo GuatemaltecoOP
I figured out it has nothing to do with Node.js v22.
In order to test the new node version, we deployed new vercel projects instead of modifying existing ones.
All new projects come with "Vercel Authentication" turned on by default in Settings > Deployment Protection. This is somehow interfering with the middleware, so just turning it off fixes the problem. Won't be investigating any further as we do not need the authentication feature currently
Answer