Categories
Node.js Quick Tips Quick Tips

The Fetch API in Node.js

< 1 min read

This blog post is part of What’s new in Node.js core? March 2022 edition.

The Fetch API is the de facto web API for making HTTP requests, and the node-fetch library has long been the go-to implementation for anyone wanting to use it in Node.js. That’s set to change with the introduction of an experimental Fetch implementation in Node.js Core.

The Fetch API can be enabled be passing the --experimental-fetch flag to node.

When the Fetch API is enabled, the following global functions and classes are made available: fetch(), Request, Response, Headers, FormData

Contributed by Michaël Zasso, Ethan Arrowood, Robert Nagy, Matteo Collina, Undici contributors

Example

const response = await fetch("https://jsonplaceholder.typicode.com/todos/1");

const responseData = await response.json();

console.log({ responseData });

Support in Node.js

The Fetch API is implemented by Deno and all modern web browsers.

When can you use it in production?

Once the experimental flag is removed.

It will be in v18 — initial release 19 Apr 2022, Active LTS starts 25 Oct 2022 — but it might remain experimental for a while.