using webpack with ethers causes a "ReferenceError: fetch is not defined" error
See original GitHub issueI 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:
- Created 3 years ago
- Reactions:2
- Comments:13 (2 by maintainers)

Top Related StackOverflow Question
You can use the webpack ProvidePlugin to set the
fetchglobal variable.solves this (taken from here: https://zauner.nllk.net/post/0024-node-fetch-webpack-typescript/)