Skip to main content

Native Fetch in Node.js and Typescript

Node.js includes a fetch function since version 18. Unfortunately, this fetch function is not yet available in the Typescript types for Node.js. By including the file below in your project, you still get access to the right Typescript types.

It works by reusing the typings from the browser environment. Because it's defined in its own file, the types do not leak into the rest of the app.

fetch.ts
/// <reference lib="dom" />
// This file allows us to use Node's native `fetch` function in Typescript.
export default fetch;