As of Node v18, which is an LTS release, we now have
fetch as a global in node.
fetch does not strictly adhere to the WHATWG fetch spec - that would be make it far less useful.
It seems to have the expected obvious differences in regards to giving you full access to headers and cookies, manual cache management, etc.
The one gotcha is that garbage collection rules aren’t so lax. You must consume the response
body manually (which is the norm in node).
In other words: the behavior mirrors the https module, but with different internals and a different API.
These are the Docs you’re looking for
undici.fetch(input[, init]): Promise:
Node fetch vs WHATWG fetch
And although it’s listed in the official Node docs in the Globals section, it’s by name only. The description is entirely empty:
This is important because there are always going to be significant differences between client-side and server-side request libraries and implementations - because the client-side has to be much more restrictive.
For security, the Browser has to be much more restrictive. For example:
- only certain headers are allowed to be modified -
- cookies, and their storage mechanisms, are opaque
- caching rules and location are opaque
fetch is a WHATWG standard, the language is very browser specific - all of those same restrictions.
Obviously, those restrictions are unfavorable for a server-side environment… but with the official Node docs silent… What are the differences?
Well, your educated guesses of what those differences are are probably right in every case, but guesses are never as nice as seeing it in writing.
And, as it turns out, Node’s implementation of
fetch is part of broader effort to rewrite the HTTP stack, code named “Undici”. That’s where the documentation lives:
Node fetch vs Node https
The internals are completely new.
It’s part of the complete from-scratch rewrite code-named “Undici”:
To make that easy to remember next time you go looking for the docs:
Undici means eleven in Italian. [HTTP] 1.1 -> 11 -> Eleven -> Undici. It is also a Stranger Things reference.