Build1 distinct publisher3 min readPublished
The failure depends on your Next.js version, your platform and the query itself, so it can pass locally and misbehave in one region; opting middleware into the Node runtime fixes it and hands back the edge placement you adopted middleware for.
The Engineer · Build desk

Compiled by The EngineerSomething wrong?How this is made
The build-time version of this bug is the good outcome. The bundler notices an unsupported Node.js API and the build fails, so you learn about it before anyone routes traffic through it [4]. The second version is a runtime error that only surfaces in production, not locally [5]. The third is the one that costs money: depending on caching and how the request happens to be handled, the query appears to work in casual testing, then behaves inconsistently under real load or in a different deployment region [6].
That inconsistency is the actual hazard, because a bug that fails identically every time is far easier to catch, as the dev.to post argues [14]. Which of the three you get depends on your Next.js version, your deployment platform and what the query specifically does, and none of them arrives as a clear statement that Mongoose does not run here [7]. A message like that would have told you what was actually broken.
The mechanism underneath is straightforward. Middleware runs on the Edge Runtime by default, close to the visitor rather than in one central location [1]. That runtime does not carry the full Node.js API surface: no `fs`, and limited or absent support for some of the networking and crypto APIs Node provides natively [2]. Mongoose and the MongoDB Node.js driver beneath it were written assuming a full Node environment, and they use APIs and networking behaviour the Edge Runtime does not fully support [3].
The escape hatch is one config line. `export const config = { runtime: 'nodejs' }` in `middleware.ts` opts the file out of the Edge Runtime and restores full Node API support [8]. According to the post, that genuinely fixes compatibility, at the price of the edge placement: middleware then runs in a more traditional serverless environment, which can mean measurably slower execution for a globally distributed audience [9].
That tradeoff claim is missing something: the post offers no latency measurement for either side [13]. For "measurably slower" to matter in your system, three things have to hold: middleware sits on the hot path of most requests, your users are far from whatever region your serverless functions run in, and the middleware's own work is not already dominated by the database round trip you are about to remove. If your audience is one country and your database lives in one region, the edge was never buying you much.
The alternative pattern avoids the choice. A signed JWT verifies from its signature alone, with no database round trip, and that verification runs fine at the edge [10]. The freshness questions, whether the user still exists and whether permissions changed since the token was issued, move into the layout or page, which runs in normal Node with no runtime restrictions [11]. The consequence is a coverage requirement rather than a code change: every protected route now has to sit under a layout that actually performs that query, because the edge check only proves the token was signed. Middleware only decides where a request goes; the database call inside it does not add anything the layout cannot do more safely.
Ranked by verification strength, evidence, and original report placement.
One failure mode of a Mongoose query in middleware is a build-time error about an unsupported Node.js API being used.
Another failure mode is a runtime error that only surfaces in production, not locally.
A third failure mode: depending on caching and how the request happens to be handled, the query appears to work in casual testing and then behaves inconsistently under real load or on a different deployment region.
Which failure occurs depends on the exact Next.js version, deployment platform, and what the query does, and none of the failures is a clean, obvious error message stating that Mongoose does not work in this environment.
Next.js middleware runs on the Edge Runtime by default, a deliberately restricted environment built for speed that runs geographically close to the visitor rather than in one central server location.
The Edge Runtime does not support the full Node.js API surface: no fs module, and limited or no support for certain networking and crypto APIs that Node.js provides natively.
Distinct publishers with included, body-backed reporting in this cluster.
1 article · September 6, 2026
Follow any of these and your For You feed starts watching them — no settings page required.
build
Your JWT Login Probably Has Exactly One Kill Switch: Log Everyone Out1 distinct publisher
build
Next.js checks Origin for Server Actions. Move the same logic to a route handler and it stops.1 distinct publisher
build
Cold starts empty the module-level Map that the API used as its cache1 distinct publisher
build
A dropper that runs on folder-open, and why your blocklist never sees it1 distinct publisher
Evidence-backed comparisons of source perspectives and observed adoption signals. Read the methodology
Which Builder, Operator, and Investor concerns the observed source mix emphasized—not a truth score.
Evidence, demonstrated adoption, hype gap, incentives, and confidence are assessed independently, each on its own current evidence. How these are measured.
One practitioner, no artifacts
The mechanism holds up on its own: the Edge Runtime's missing APIs and the runtime: 'nodejs' export are framework behaviour any reader can check against Next.js itself. The failure taxonomy is a different quality of evidence, offered with no build log and no pinned version even though the post says the outcome turns on version and platform. Four illustrative snippets are not a reproduction.
No usage signal
Nothing in this reporting counts how many projects put a driver in the middleware import graph, and no release, deployment or platform disclosure accompanies it. Reading one developer's anecdote as prevalence would be inventing the measurement, so we decline to score it.
Firmer in summary than in the body
The post hedges constantly, 'can fail', 'sometimes', 'depending on', while the takeaway hardens into three ways it breaks. The genuinely stretched word is 'measurably', which does the job a benchmark would have done. Modest overstatement, and the incompatibility underneath it is real.
Advice with a storefront attached
The piece signs off with a link to the author's paid Next.js templates and an invitation to describe your own outage in the comments, which is engagement that shop benefits from. The recommended pattern itself sells nothing, so the conflict shapes the urgency of the telling rather than the substance of the fix, and it is disclosed in plain sight.
Act on the boundary, test the taxonomy
We would treat the runtime boundary and the two-part auth pattern as reliable, since both are verifiable against the framework in an afternoon. The three failure modes and their relative frequency we would treat as a hypothesis worth checking in your own build, because a single uncorroborated post is the whole basis for them.