Next.js Discord

Discord Forum

How to create a middleware logger without running into edge runtime issues?

Unanswered
togakangaroo posted this in #help-forum
Open in Discord
Hi, I am on Next14 running in our own EKS cluster (not vercel), trying to create a very simple request/response logger-to-stdout. While I can get this working, I am not able to use the simple logger facade I created because it references process.cwd() in order to figure out the root directory and make logging of the current filename more relevant. Whenever this line is called I get an error
A Node.js API is used (process.cwd) which is not supported in the Edge Runtime.
Learn more: https://nextjs.org/docs/api-reference/edge-runtime

This link is deeply unhelpful for this error as its just a list of apis, not an explanation of the error or even of what the edge runtime feature is, however I've been able to gather that it has something to do with wanting middleware to run on edge nodes and hence there's limited node apis available (nor would process.cwd() give relevant info)

The whole idea of running on edge servers however makes weird sense for a logger (since they would have their own logger), yet a logger is both one of the main examples of what you should use it for, and this seems to be the only way of actually implementing a request/response logger. So what can I do? This isn't running on edge servers. I have no desire for this to ever run on edge servers (it would make no sense for our app which isn't direct-to-consumer). How can I tell next to just chill out and use the normal node runtime?

0 Replies