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
x
42
42
1
import CountryBtn from './components/CountryBtn.js'
2
import WeatherIcon from './components/WeatherIcon.js'
3
import fetch from 'node-fetch'
4
5
6
const App = () => {
7
return (
8
<>
9
<div id='WeatherForm'>
10
<div id='CountryBtnGroup'>
11
<CountryBtn name='Seoul' />
12
<CountryBtn name='Moscow' />
13
</div>
14
<div id='WeatherToday'>
15
<h2>Today</h2>
16
<div id='TemperatureToday'><WeatherIcon type='Today' /><h2>15℃</h2></div>
17
</div>
18
<div id='WeatherOtherDay'>
19
<div id='Today+1' className='AnotherDay'>
20
<h2>AnotherDay</h2>
21
<div className='TemperatureAnother'><WeatherIcon type='AnotherDay' /><h2>14℃</h2></div>
22
</div>
23
<div id='Today+2' className='AnotherDay'>
24
<h2>AnotherDay</h2>
25
<div className='TemperatureAnother'><WeatherIcon type='AnotherDay' /><h2>14℃</h2></div>
26
</div>
27
<div id='Today+3' className='AnotherDay'>
28
<h2>AnotherDay</h2>
29
<div className='TemperatureAnother'><WeatherIcon type='AnotherDay' /><h2>14℃</h2></div>
30
</div>
31
<div id='Today+4' className='AnotherDay'>
32
<h2>AnotherDay</h2>
33
<div className='TemperatureAnother'><WeatherIcon type='AnotherDay' /><h2>14℃</h2></div>
34
</div>
35
</div>
36
</div>
37
</>
38
)
39
}
40
41
export default App
42
Some of the errors returned are:
What I tried to resolve the errors are:
- Trying to substitute fetch with axios. But it seems to return the same errors.
- 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.