Next.js Discord

Discord Forum

Using node modules in middleware

Answered
Sun bear posted this in #help-forum
Open in Discord
Sun bearOP
I'm setting up a middleware function that checks authentication before accessing specific routes. My authentication code contains references to modules that use node: modules (node:url specifically). When building, I get the following error:
Module build failed: UnhandledSchemeError: Reading from "node:url" is not handled by plugins (Unhandled scheme).

I did some research, and it looks like this may be because middleware are being compiled for the edge environment, not node. However, I'm not planning on hosting on a platform that runs middleware on the edge. Is there a way to compile middleware for the node environment instead?

Some related discussions I found while looking into this:
https://github.com/vercel/next.js/issues/28774
https://github.com/vercel/next.js/discussions/47684
https://github.com/vercel/next.js/discussions/47314
Answered by Sun bear
Thanks! I ended up creating a route handler and calling that with fetch(). It doesn't feel ideal but.. I guess it is what it is 😛
View full answer

2 Replies