cross-fetch icon indicating copy to clipboard operation
cross-fetch copied to clipboard

Not working on cloudflare worker

Open tttp opened this issue 4 years ago • 8 comments

Hi,

Cloudflare worker provides a fetch function, however, when I'm webpacking your lib, it does seem to use XMLHttpRequest

ReferenceError: XMLHttpRequest is not defined

I did try to force webpack to use node

module.exports = {
  target: "webworker",
  entry: "./worker.js", // inferred from "main" in package.json
  resolve: {
    mainFields: ['module', 'main']
  }
};

But it generates another error (global not defined)

Is there a way to force cross-fetch to to use the native fetch function?

as a proof of concept/fugly workaround, I replaced

const Fetch = fetch; //require('cross-fetch');

and it seems to work fine.

tttp avatar Jul 24 '20 11:07 tttp

I'm hitting this as well because https://www.npmjs.com/package/prismic-javascript is using cross-fetch and I am doing things in Cloudflare workers.

In order to work around the lack of support for workers I added the following to my webpack.config.js:

  externals: [
    {
      'cross-fetch': 'fetch'
    }
  ]

That essentially replaces this whole module with the native fetch.

I still think cross-fetch should not try to polyfill what already exists in the environment it runs in. I think experience shows this is not good practice

Munter avatar Mar 22 '21 16:03 Munter

~@lquixada This issue has been open for a while with no resolution. It appears the issue is that this repo overrides fetch if global.fetch is undefined. Cloudflare workers do not have a global object defined~

The issue actually looks more complex than I thought. Is it on the roadmap to fix this issue?

joshmsamuels avatar Oct 03 '21 00:10 joshmsamuels

👀 https://github.com/lquixada/cross-fetch/blob/main/dist/browser-ponyfill.js#L546

Uncommenting that line and commenting out the one below fixed my issue

joshmsamuels avatar Oct 03 '21 01:10 joshmsamuels

We've been testing the supabase libs with the new Next.js middleware and found that it's also encountering this bug.

I can confirm that the patch @joshmsamuels has made fixes it.

@lquixada - I'm not familiar enough with this repo to know the repercussions of this change. However if you would like me to submit a PR with the change let me know

kiwicopple avatar Nov 22 '21 07:11 kiwicopple

@joshmsamuels the link to your fix seems to 404 now. Would you be willing to put up a PR?

rattrayalex avatar Mar 01 '22 19:03 rattrayalex

The link is 404'ing since the dist folder that I was referencing was deleted along with all the other dist files at the beginning of the year.

It looks like that was also how browser fetch was supported so I am unsure what @lquixada's plan was as the owner and maintainer of the repo.

joshmsamuels avatar Mar 02 '22 00:03 joshmsamuels

Got it. Maybe I will make a fork to support this and other features, since it seems this repo isn't so actively maintained anymore…

rattrayalex avatar Mar 02 '22 01:03 rattrayalex

Any updates on this?

SokratisVidros avatar May 09 '22 09:05 SokratisVidros

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

lquixada avatar Jun 09 '23 22:06 lquixada

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

Thanks so much. Thanks to your 4.x branch, I was able to port a dependency I wanted to use (@logtail/node) to Cloudflare Workers — it's working great so far. I'll let you know if I run into any issues.

UPDATE: while the code seems to work fine, it looks like the 4.x branch still has a dependency on node-fetch, which is causing typescript issues: https://github.com/lquixada/cross-fetch/blob/v4.x/package.json#L71

aroman avatar Jun 17 '23 06:06 aroman

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

lquixada avatar Jul 03 '23 17:07 lquixada