lquixada / cross-fetch

Universal WHATWG Fetch API for Node, Browsers and React Native.
MIT License
1.67k stars 105 forks source link

Cross fetch throw an error [ReferenceError: XMLHttpRequest is not defined] #142

Closed yasirrafiq33 closed 1 year ago

yasirrafiq33 commented 2 years ago

This is specific case with nextJs middleware. The package is working fine everywhere but when we try to use it in NEXTJS middleware its throw error. The middleware is using edge runtime. https://nextjs.org/docs/api-reference/edge-runtime

bebejane commented 1 year ago

I'm seeing the same error with Vercel edge api functions https://nextjs.org/docs/api-routes/edge-api-routes

perry-mitchell commented 1 year ago

Seeing the same issue with background (service worker) scripts in Chrome extensions running on the new manifest V3. Why am I seeing XMLHttpRequest in cross-fetch?

XMLHttpRequest is not defined

Tomas2D commented 1 year ago

Does anyone resolves it?

perry-mitchell commented 1 year ago

Gave up waiting, released my own.

Tomas2D commented 1 year ago

@perry-mitchell , I solved it this way

https://github.com/Tomas2D/pagerduty-calculator/blob/main/src/polyfill.ts https://github.com/Tomas2D/pagerduty-calculator/blob/main/vite.config.ts

perry-mitchell commented 1 year ago

@perry-mitchell , I solved it this way

https://github.com/Tomas2D/pagerduty-calculator/blob/main/src/polyfill.ts

https://github.com/Tomas2D/pagerduty-calculator/blob/main/vite.config.ts

But then you're not really using fetch 😅. I needed it in my case as I need to support service workers, which don't provide a working XHR.

Tomas2D commented 1 year ago

You are right, mine is just for NodeJS / Browser environment, not workers. I will use your solution, once I will need to make it work in service workers 👍🏻

perry-mitchell commented 1 year ago

Your solution is completely valid of course if the interface of fetch is all that matters. Cheers 👍

nwarpy commented 1 year ago

I've created a way to disable cross-fetch if you don't need it.

https://github.com/nwarpy/disable-cross-fetch

mellunar commented 1 year ago

This issue also affects Cloudflare Wrangler

Mnigos commented 1 year ago
Screenshot 2023-06-01 at 15 53 05

I also have this issue

lquixada commented 1 year ago

I've been working on version 4 of cross-fetch to fix this issue. If anyone's interested, please run npm install cross-fetch@latest-v4.x in your project and give it a try. Let me know if any issues come up.

lquixada commented 1 year ago

Version 4 has been officially released with the fix. Please check it out: npm install cross-fetch.