`nuxt dev` answers every SSR request with `Cannot find module 'ofetch'` from `fetch.server.mjs` when the first request arrives during warm-up (pnpm isolated linker)
Environment
| Operating system | macOS 27.0.0, arm64 |
| Node.js | v24.19.0 |
| Package manager | pnpm 11.22.0, default node-linker (isolated) |
| Nuxt | [email protected] (current main) and 5.0.0-29835210.f258408a |
| Nitro | 3.0.260903-beta |
| Builder | vite 8.3.0 |
| nuxt/cli | 4.0.0-20260922-145010-cf0e756 (what the nightly installs today) and 4.0.0-20260820-224244-a3dee96 |
Reproduction
Self-contained — three files and a script, no repository needed. Run it in a directory with no
node_modules/ofetch in any parent directory (see Additional context).
package.json
{
"private": true,
"type": "module",
"dependencies": {
"nuxt": "npm:[email protected]",
"vue": "3.5.43",
"vue-router": "5.3.1"
}
}nuxt.config.ts
export default defineNuxtConfig({
devtools: { enabled: false },
telemetry: false,
})app/app.vue
<template>
<p>hello</p>
</template>repro.sh
#!/bin/sh
# Cold-starts `nuxt dev` and sends GET / every 50 ms from launch; prints the first answer that is
# not 503 (the loading page), then a later one. DELAY=6 waits six seconds before the first request.
PORT=${PORT:-4611}
rm -rf .nuxt node_modules/.cache
./node_modules/.bin/nuxt dev --port "$PORT" > dev.log 2>&1 &
sleep "${DELAY:-0}"
while :; do
code=$(curl -s -o /dev/null -w '%{http_code}' "http://localhost:$PORT/")
[ "$code" != 000 ] && [ "$code" != 503 ] && break
sleep 0.05
done
echo "first answer: $code"
sleep 2
echo "later answer: $(curl -s -o /dev/null -w '%{http_code}' "http://localhost:$PORT/")"
kill $!pnpm install
./repro.sh # first answer: 500 / later answer: 500
DELAY=6 ./repro.sh # first answer: 200 / later answer: 200Describe the bug
When the first request reaches nuxt dev while it is still warming up, every SSR request fails
with Cannot find module 'ofetch' imported from 'virtual:nuxt:.nuxt%2Ffetch.server.mjs' for the
rest of the process's life (still 500 a minute later here). A restart recovers only when its first
request comes late: a first request a few seconds after launch gets 200, and so does the
production build.
Measured with a harness equivalent to repro.sh (cold start each time, starts interleaved across
versions, two series):
| version | early first request | first request after 6 s |
|---|---|---|
5.0.0-29836693.3006e5ac (main) |
500 in 20 of 20 | 200 in 3 of 3 |
5.0.0-29835210.f258408a |
500 in 20 of 20 | 200 in 3 of 3 |
5.0.0-29815057.84261aa3 (#36318) |
500 in 5 of 5 | — |
5.0.0-29814795.2923df84 (its parent) |
200 in 5 of 5 | — |
5.0.0-29787188.1d507ee7 |
200 in 20 of 20 | 200 in 3 of 3 |
f258408a + publicHoistPattern: ['ofetch'] |
200 in 20 of 20 | — |
f258408a + ofetch as a direct dependency |
200 in 5 of 5 | — |
It starts at #36318 (84261aa3, the Nitro v2 compatibility layer):
packages/nitro-server/src/runtime/compat/decorate.ts imports createFetch from ofetch, and
handlers/renderer.ts imports decorateLegacyEvent from it. Both lines are unchanged on main.
The mechanism, as far as I can trace it:
- Vite caches the dev SSR externalization verdict per specifier, not per importer
(
createIsExternalinpackages/vite/src/node/external.ts:processedIdsis keyed by the bare id, andisExternalizableresolves it from whichever importer asks first). - If
@nuxt/nitro-server'sruntime/compat/decorate.mjsis analysed first,ofetchresolves from inside that package and is cached as external. - The virtual
fetch.server.mjstemplate (import { createFetch } from 'ofetch') then keeps a bare external import. Its importer is virtual, so the module runner resolvesofetchfrom the project root, where pnpm's isolated linker puts noofetch—ERR_MODULE_NOT_FOUND. - If
fetch.server.mjsis analysed first,ofetchdoes not resolve from the root, is cached as not external, and every importer'sofetchis resolved to a file and inlined — the virtual one throughnuxt:resolve-bare-imports, from Nuxt's own directory. That was the only order before #36318, and it is still the order when the first request comes late.
A transform trace (a transform/resolveId logger added with addVitePlugin, times from module
setup) shows the two orders on f258408a:
early first request -> 500
508ms env=ssr @nuxt/nitro-server-nightly/dist/runtime/compat/decorate.mjs
755ms env=ssr virtual:nuxt:.nuxt%2Ffetch.server.mjs
(no resolveId call for 'ofetch' in the ssr environment: both imports were externalized)
first request after 6 s -> 200
304ms env=ssr virtual:nuxt:.nuxt%2Ffetch.server.mjs
313ms resolveId 'ofetch' env=ssr importer=virtual:nuxt:.nuxt%2Ffetch.server.mjs
576ms env=ssr @nuxt/nitro-server-nightly/dist/runtime/compat/decorate.mjs
579ms resolveId 'ofetch' env=ssr importer=.../runtime/compat/decorate.mjsAdditional context
- A
node_modules/ofetchin any parent directory hides the bug: the root resolution walks up and finds it. The same app placed under a directory whosenode_moduleshadofetchanswered200in 5 of 5 early starts. @nuxt/nitro-serveralready listsofetchinnitroImplicitDependenciesand routes those specifiers to Nitro's copy through aresolveIdfallback for package managers that do not hoist. An import Vite has already externalized never reaches aresolveIdhook: Vite'sfetchModuleresolves it withtryNodeResolvefrom the importer, and a virtual importer falls back to the project root.- Workarounds that hold here:
publicHoistPattern: ['ofetch']inpnpm-workspace.yaml, orofetchas a direct dependency. - The production build is unaffected: early requests to
node .output/server/index.mjsanswer200(3 of 3), withofetchbundled into the SSR chunk.
Logs
◆ Ready in 309ms
│ config 71ms · modules 15ms · app 56ms · types 113ms · bundle 10ms · server 44ms
[log] [log] [request error] [GET] http://localhost:4611/
✖ HTTPError [ERR_MODULE_NOT_FOUND] (500): Cannot find module 'ofetch' imported from
'virtual:nuxt:.nuxt%2Ffetch.server.mjs'
… 10 dependency framesSource: nuxt/nuxt