Skip to main content

Node polyfills

Any version prior to Node 18 will need to provide polyfills.


The Tableland SDK makes use of the fetch API as well as Headers. If you're using a version of Node that came before the current LTS 18.x version, you'll need to either update to Node 18 or implement a polyfill.

Installation

To use a polyfill, you'll first need to install node-fetch.

npm install node-fetch

Starting with node-fetch v3, it is an ESM-only module; you are not able to import it with require(). If you cannot switch to ESM and need CommonJS (CJS) support, you could use v2 (node-fetch@2)—or choose to use an async import().

Add the polyfill

In your source file, import fetch, Headers, Request, and Response from node-fetch. Then, you'll patch the global object in Node (globalThis), thus, enabling Tableland SDK compatability.

import fetch, { Headers, Request, Response } from "node-fetch";

if (!globalThis.fetch) {
globalThis.fetch = fetch;
globalThis.Headers = Headers;
globalThis.Request = Request;
globalThis.Response = Response;
}

Note that if you're using TypeScript, you can just declare globalThis as any in the ESM example declarations above (i.e., add a type to all of the references to globalThis).