Skip to content
Advertisement

React returns several errors when I import fetch

I am building a simple app using React. The app works fine without any issue until I import fetch.

Here is the code I ran:

App.js

JavaScript

Some of the errors returned are:

enter image description here

What I tried to resolve the errors are:

  1. Trying to substitute fetch with axios. But it seems to return the same errors.
  2. Adding experiments: { topLevelAwait: true } to the module.exports object in webpack.config.js file.

I made a research in google but I could not find any solution to fix this issue. How can I fix this issue?

Advertisement

Answer

I have solved the issue. It seems like node-fetch packages that have higher versions than node-fetch@2.x.x are not compatible with React. So I downgraded the node-fetch package into node-fetch@2.6.1. Now the app runs well without any issue!

I found the solution at https://github.com/vercel/next.js/discussions/33982.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement