Hello! I used new nodeMiddleware with canary build. Worked ok, I made some production deployments with it. After I created preview deployment it started to error in every middleware invocation with error
TypeError: middlewareHandler is not a function
at /var/task/___next_launcher.cjs:116:30
at AsyncLocalStorage.run (node:internal/async_local_storage/async_hooks:91:14)
at withNextRequestContext (/var/task/___next_launcher.cjs:30:18)
...
I would expect it not to crash, Locally my builds doing great
Hey @kirill-x. There’s not enough info here for me to be sure of what’s causing the error. I wrote another post with some suggestions for what details are helpful to include.
Please take a look and then add whatever you can here to help the rest of us see what’s going wrong
Hey! Im not sure what details are needed for this issue.
Here is error that is displayed instead of my preview website on the routes, that are triggering middleware
This is the full stacktrace from logs on vercel dashboard
TypeError: middlewareHandler is not a function
at /var/task/___next_launcher.cjs:116:30
at AsyncLocalStorage.run (node:internal/async_local_storage/async_hooks:91:14)
at withNextRequestContext (/var/task/___next_launcher.cjs:30:18)
at Object.serve [as handlerWeb] (/var/task/___next_launcher.cjs:113:18)
at Object.handler (/opt/rust/nodejs.js:2:14544)
at Server.<anonymous> (/opt/rust/nodejs.js:2:1716)
at /opt/rust/nodejs.js:16:6144
at AsyncLocalStorage.run (node:internal/async_local_storage/async_hooks:91:14)
at /opt/rust/nodejs.js:16:6132
at AsyncLocalStorage.run (node:internal/async_local_storage/async_hooks:91:14)
Thanks for sharing the error info and deployment id. From what I can see in the logs, it looks like that build used an older Next.js canary version. Does the TypeError still happen if you switch to either latest or canary to make sure the most recent updates are included?
I had in package.json exact version that currently works fine in production deployment.
I updated version in preview branch to canary now, error is still there.
Never mind. I figured it out, hahaha. I moved my middleware.ts file inside the app folder. Previously, it was located in the root directory, and there was no problem with that locally, but Vercel couldn’t detect it. It seems Vercel can only detect it if it’s inside the app folder. That solved it for me.
I dont think I can make an example. I merged my branch to main and now problem is gone. Not sure what it was by it is related to how vercel treat barnch deploymnets
The main difference between preview deployments and production is whether NODE_ENV=production or not by default
You can try setting that explicitly in your environment variables to override and see if that helps you reproduce the scenario
If it is an issue that only breaks when in non-production, then double check whether your Next modules are in regular “dependencies” or “devDependencies”
TypeError: middlewareHandler is not a function
at /var/task/___next_launcher.cjs:116:30
at AsyncLocalStorage.run (node:async_hooks:346:14)
at withNextRequestContext (/var/task/___next_launcher.cjs:30:18)
at Object.serve [as handlerWeb] (/var/task/___next_launcher.cjs:113:18)
at Object.handler (/opt/rust/nodejs.js:2:14544)
at Server.<anonymous> (/opt/rust/nodejs.js:2:1716)
at /opt/rust/nodejs.js:16:6144
at AsyncLocalStorage.run (node:async_hooks:346:14)
at /opt/rust/nodejs.js:16:6132
at AsyncLocalStorage.run (node:async_hooks:346:14)