question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

using webpack with ethers causes a "ReferenceError: fetch is not defined" error

See original GitHub issue

I created a Typescript lambda function using serverless-bundle which uses webpack to efficiently bundle the typescript. One of my dependencies is ethers. In my function I try to fetch a block with provider.getBlock("latest"), and it fails with a missing response error. Digging a little deeper into the error reveals the true cause of the error

ReferenceError: fetch is not defined

I think when ethers gets bundled by webpack, it thinks it’s going to be used in a browser environment and emits the call to fetch, which doesn’t work when it’s run in a lambda function.

I saw the previous issue #1108, and tried adding mainFields: ["main"] to my webpack.config.js but it still errors out.

How do I coerce webpack to not cause the fetch function to be emitted in the bundled code?

Original GitHub issue in serverless-bundle https://github.com/AnomalyInnovations/serverless-bundle/issues/204

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:13 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
pulasthibandaracommented, Nov 9, 2021

You can use the webpack ProvidePlugin to set the fetch global variable.

plugins: [
    new webpack.ProvidePlugin({
      WebSocket: 'ws',
      fetch: ['node-fetch', 'default'],
    }),
]
2reactions
mwawruschcommented, Apr 3, 2021
import fetch from "node-fetch";
// tslint:disable-next-line:no-any
declare var global: any;
global.fetch = fetch;

solves this (taken from here: https://zauner.nllk.net/post/0024-node-fetch-webpack-typescript/)

Read more comments on GitHub >

github_iconTop Results From Across the Web

ReferenceError: fetch is not defined - javascript - Stack Overflow
It seems fetch support URL scheme with "http" or "https" for CORS request. Install node fetch library npm install node-fetch , read the...
Read more >
Webpack 5 Issues | Documentation - Web3Auth
This issue is caused due to the fact that the web3.js and ethers.js packages have certain dependencies, which are not present within the...
Read more >
ReferenceError: fetch is not defined in NodeJs | bobbyhadz
The "ReferenceError: fetch is not defined" occurs when the fetch() method is used in an environment where it's not supported - most commonly...
Read more >
Documentation - Ethers.js
This adds a large delay to any code that was currently running. This can be exploited by attackers to "condition cause a delay"....
Read more >
How to fix 'ReferenceError: fetch is not defined' in Node.js
First, let's replicate the issue. You can update the index.js to the following and run node index.js , you should be able to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found