Skip to content

Error: fetch is not defined #15

@swiiny

Description

@swiiny

Describe the bug
When running the client in a Node.js environment, the fetch is not defined error occurs while trying to get draw results.

To Reproduce
Steps to reproduce the behavior:

  1. Call getUsersDrawResultsForDrawId() (see this tutorial at step 6.3)
const drawResults = await prizeDistributor.getUsersDrawResultsForDrawId(
	usersAddress,
	draw.drawId,
	prizeDistribution.maxPicksPerUser
);
  1. See error in terminal
my-project-name/node_modules/@pooltogether/v4-client-js/dist/v4-client-js.cjs.development.js:2072
              return fetch(this.getDrawResultsStatusUrl(chainId, prizeDistributorAddress, drawId));
              ^

ReferenceError: fetch is not defined

Expected behavior
The expected behavior was to get draw results of a user

How I quickly fix this to make it works

  1. Run npm i node-fetch
  2. Add these lines at the beginning of my entry index.js file
import fetch from 'node-fetch';
globalThis.fetch = fetch;

Desktop

  • OS: macOS Monterey (12.3.1)
  • Node: 16.16.0
  • NPM: 8.11.0

Additional context
I believe this error is occurring because the client is being run in a Node.js environment instead of a browser. However, this issue may be resolved with the new Node.js v18 version, as they have introduced a fetch API. It is also worth noting that the package.json file indicates that the client should work on Node.js versions equal to or greater than 16.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions