node.jsfetch-apinode-fetch

why is the agent option not available in node native fetch?


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


Solution

  • 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.