unjs / nitro

Next Generation Server Toolkit. Create web servers with everything you need and deploy them wherever you prefer.
https://nitro.unjs.io
MIT License
6.13k stars 503 forks source link

_fetch is not a function. In build #1635

Closed LiNCH35 closed 9 months ago

LiNCH35 commented 1 year ago

Environment

Nitro: v2.6.0 Node: v16.16.0

NITRO_PRESET=node_cluster

Reproduction

Only build mode. I couldn't reproduce

Describe the bug

Downgrading to 2.5.2 or disabling node_cluster may help

Additional context

No response

Logs

TypeError: _fetch is not a function
0|Front2  | 2023-08-24T18:47:12:     at ufetch (file:///var/www/a_front2/.output/server/index.mjs:3002:14)
0|Front2  | 2023-08-24T18:47:12:     at localFetch (file:///var/www/a_front2/.output/server/index.mjs:17261:12)
0|Front2  | 2023-08-24T18:47:12:     at $fetchRaw2 (file:///var/www/a_front2/.output/server/index.mjs:2877:32)
0|Front2  | 2023-08-24T18:47:12:     at $fetch2 (file:///var/www/a_front2/.output/server/index.mjs:2919:21)
0|Front2  | 2023-08-24T18:47:12:     at useVzFetch (file:///var/www/a_front2/.output/server/chunks/app/server.mjs:4920:10)
0|Front2  | 2023-08-24T18:47:12:     at file:///var/www/a_front2/.output/server/chunks/app/server.mjs:7694:12
0|Front2  | 2023-08-24T18:47:12:     at _loadData (file:///var/www/a_front2/.output/server/node_modules/@vueuse/core/index.mjs:4544:20)
pi0 commented 1 year ago

Hi. I cannot reproduce this issue using Node.js 16 and cluster preset on repo with pnpm nitro build --preset node_cluster test/fixture.

Can you please remove your lockfile and reinstall all node_modules. We had some small fixes for fetch polyfill in node-fetch-native you might be using a stalled one.

BTW please consider to migrate to Node.js v18 sooner as v16 is getting EOL very soon and node 18 has built-in fetch.

LiNCH35 commented 1 year ago

Hi. We put the project on an empty virtual machine with node 16 and node 18. The result was the same. Tried to reproduce purely nitro and really did not give errors. Maybe the problem is nuxt when it uses nitro ^2.6.0? Here is a similar problems: https://github.com/nuxt/nuxt/issues/22816 https://github.com/nuxt/nuxt/issues/22783

I will try to reproduce it later

pi0 commented 1 year ago

It is strange if _fetch does not work also on Node.js 18! Thanks for explaining and putting time to make references. Feel free to share any updates you have here to help fixing it ASAP.

LiNCH35 commented 1 year ago

I don't know if this will help: On version 2.5.2 in unenv global has the following fields 'fetch', 'Blob', 'File', 'FormData', 'Headers', 'Request', 'Response' on version 2.6.0 in.output/server/index.mjs in global does not have these fields.

Method createFetch

MiniDigger commented 1 year ago

I am running into this too and managed to create a repro:

  1. download https://stackblitz.com/edit/github-7rplqy
  2. npm i
  3. npm run previewBuild
  4. curl localhost:3000 see error:
    
    > preview
    > node .output/server/index.mjs

Listening on http://[::]:3000 route called [nitro] [request error] [unhandled] [GET] "http://localhost:3000/dum": _fetch is not a function at async $fetchRaw2 (/C:/Gothaer/Download/github-7rplqy/.output/server/index.mjs:2765:14) at async $fetch2 (/C:/Gothaer/Download/github-7rplqy/.output/server/index.mjs:2798:15) at async /C:/Gothaer/Download/github-7rplqy/.output/server/chunks/index.mjs:19:3 at async Object.handler (/C:/Gothaer/Download/github-7rplqy/.output/server/index.mjs:2315:19) at async Server.toNodeHandle (/C:/Gothaer/Download/github-7rplqy/.output/server/index.mjs:2504:7) [h3] [unhandled] FetchError: [GET] "http://localhost:3000/dum": _fetch is not a function at async $fetchRaw2 (file:///C:/Gothaer/Download/github-7rplqy/.output/server/index.mjs:2765:14) at async $fetch2 (file:///C:/Gothaer/Download/github-7rplqy/.output/server/index.mjs:2798:15) ... 2 lines matching cause stack trace ... at async Server.toNodeHandle (file:///C:/Gothaer/Download/github-7rplqy/.output/server/index.mjs:2504:7) { cause: TypeError: _fetch is not a function at ufetch (file:///C:/Gothaer/Download/github-7rplqy/.output/server/index.mjs:2880:14) at localFetch (file:///C:/Gothaer/Download/github-7rplqy/.output/server/index.mjs:5263:12) at $fetchRaw2 (file:///C:/Gothaer/Download/github-7rplqy/.output/server/index.mjs:2756:32) at onError (file:///C:/Gothaer/Download/github-7rplqy/.output/server/index.mjs:2695:16) at $fetchRaw2 (file:///C:/Gothaer/Download/github-7rplqy/.output/server/index.mjs:2765:20) at async $fetch2 (file:///C:/Gothaer/Download/github-7rplqy/.output/server/index.mjs:2798:15) at async file:///C:/Gothaer/Download/github-7rplqy/.output/server/chunks/index.mjs:19:3 at async Object.handler (file:///C:/Gothaer/Download/github-7rplqy/.output/server/index.mjs:2315:19) at async Server.toNodeHandle (file:///C:/Gothaer/Download/github-7rplqy/.output/server/index.mjs:2504:7), statusCode: 500, fatal: false, unhandled: true, statusMessage: undefined, data: undefined }


tested on node 16.19.1. node 18.17.1 worked for me (as in, showed the 404 as I expect)
node 16 migration is still underway at work because of the glibc requirement sadly...

did some digging in the code:
adding a `console.log(globalThis.fetch)` here https://github.com/unjs/nitro/blob/f126cbfe25faf6eac445d09e27a016016674a2f3/src/runtime/app.ts#L96 logs undefined on 16 and the expected asyncfunction on 18.

downgrading to nitro 2.5.2 it works as expected again, the log in that line logs the async function on 16, so somewhere between 2.5.2 and 2.6.2 the polyfill for fetch broke I guess?
not sure where to look from here, but maybe you got some pointers @pi0 (can also hit me up on discord `minidigger` on nuxt discord)

edit: updated my dev env to node 18 (after it worked locally), still not working (same fetch is not a function), I am going home.....
1129zaj commented 1 year ago

It is strange if _fetch does not work also on Node.js 18! Thanks for explaining and putting time to make references. Feel free to share any updates you have here to help fixing it ASAP.

mo too

tasiotas commented 1 year ago

same here on Node 20 and Nitro 2.7 (worked fine on Nitro 2.6)

nuxt  | [nitro] [unhandledRejection] FetchError: [GET] "http://django:8000/bRs47jH13fdsBFMQ93/django/api/utils/sitemap": <no response> _fetch is not a function
nuxt  |     at async $fetchRaw2 (file:///app/node_modules/ofetch/dist/shared/ofetch.00501375.mjs:228:14)
nuxt  |     at async $fetch2 (file:///app/node_modules/ofetch/dist/shared/ofetch.00501375.mjs:261:15)
nuxt  |     at buildUrls (/app/utils/useSitemap.js:44:1)
nuxt  |     at useSitemap (/app/utils/useSitemap.js:243:1) {
nuxt  |   [cause]: TypeError: _fetch is not a function
nuxt  |       at ufetch (file:///app/node_modules/unenv/runtime/fetch/index.mjs:6:14)
nuxt  |       at localFetch (/app/node_modules/nitropack/dist/runtime/app.mjs:69:39)
nuxt  |       at $fetchRaw2 (file:///app/node_modules/ofetch/dist/shared/ofetch.00501375.mjs:219:32)
nuxt  |       at onError (file:///app/node_modules/ofetch/dist/shared/ofetch.00501375.mjs:158:16)
nuxt  |       at $fetchRaw2 (file:///app/node_modules/ofetch/dist/shared/ofetch.00501375.mjs:228:20)
nuxt  |       at async $fetch2 (file:///app/node_modules/ofetch/dist/shared/ofetch.00501375.mjs:261:15)
nuxt  |       at buildUrls (/app/utils/useSitemap.js:44:1)
nuxt  |       at useSitemap (/app/utils/useSitemap.js:243:1)
nuxt  | }
tasiotas commented 12 months ago

solved it, sorry it was my old code

I had this set in my Dockerfile ENV NODE_OPTIONS=\"--no-experimental-fetch\"

not removing fetch from Node fixed my issue :D

liu-huitao commented 11 months ago

Me too image

The development environment is normal, but the production environment may experience the above errors

Environment Node: v18.17.0 Nuxt 3.8.0 with Nitro 2.7.2

maxflex commented 10 months ago

Same here.

Nuxt 3.9.0 with Nitro 2.8.1. 
Node v20.10.0

upd. Downgraded to Nuxt 3.6.5 with Nitro 2.5.2 – the error is still there

xupapa commented 9 months ago

Same here. Nuxt: 3.8.2 with Nitro 2.8.1 , node version is 20.11.0

manniL commented 9 months ago

@xupapa please provide a reproduction 👍

yinfoyuan commented 9 months ago

I can't upgrade the node version yet. Is there any solution? The current version is 16.15.0

lincenying commented 9 months ago

I think I know the reason. I compiled it locally with Nodejs18, and then used scp to transfer it to the server. The nodejs version of the server is 16, so an error was reported. I also switched to nodejs16 locally and compiled it, and no errors were reported.

pi0 commented 9 months ago

I know it is a painful situation for some deployment flows but please consider:

It is not easy to track in one issue because different deployment situations are different.

If you are stuck with this issue even checking both requirements above, please use a new issue with reproduction so we can properly investigate each one by one 🙏🏼

Syinho commented 6 months ago

我的开发环境nodejs版本是20.12.2, 服务器线上nodejs版本是16, 在这种情况下会报错_fetch is not a function. 我尝试升级线上服务器的nodejs版本到20.12.2, 与开发环境保持一致, 问题就解决了. My development environment nodejs version is 20.12.2, and the server online nodejs version is 16, in which case I get an error '_fetch is not a function'. I tried upgrading the nodejs version of the live server to 20.12.2 to match the development environment, and the problem was solved.