7

I have started using the native fetch function recently (node 17+)

I realized today it is lacking a few functionalities from node-fetch, e.g. agent

Why is that?

Is there are plan to add it?

It is a shame because I needed to add node-fetch to my project as a result

see

John
  • 4,786
  • 8
  • 35
  • 44
  • 1
    Isn't the entire point of having `fetch()` built into nodejs that it is the identical API as what's in the browser? There are richer options such as `got()` or `axios()` or `node-fetch()` if you want support for node-specific behaviors. – jfriend00 Sep 22 '22 at 15:56

2 Answers2

2

The native fetch function is Experimental ie not ready for production and very likely has issues.

https://nodejs.org/dist/latest-v18.x/docs/api/globals.html#fetch

Also jfriend00 point is very valid: "Isn't the entire point of having fetch() built into nodejs that it is the identical API as what's in the browser?"

Michael Hobbs
  • 1,663
  • 1
  • 15
  • 26
1

The actual answer is to why the options you're used to from the http module aren't available is that perhaps surprisingly, node's builtin fetch() global does not use the HTTP stack provided by the traditional builtin http/https modules.

Instead, it uses a parallel, from-scratch HTTP stack rewrite called undici.

Given that fetch()'s HTTP stack is entirely separate from the standard HTTP stack, it should not be surprising that the options you can supply to http.get et al don't work with fetch().

undici's docs are available here. http Agents are replaced by a Dispatcher. You can pass a custom Dispatcher in to fetch(…, { dispatcher }), which allows you to customize fetch's HTTP behavior.

josh3736
  • 139,160
  • 33
  • 216
  • 263