"Fetch in nodejs not working? Getting 'fetch is not defined' error"
Hey guys,
So i'm trying to use fetch in nodejs for an API call, but keep getting "fetch is not defined". Wth? I thought fetch was standard now.
Turns out, nodejs doesn’t include fetch by default (ugh). Anyone else run into this?
What’s the easiest fix? Do i need to install something like node-fetch or axios? Or is there a better way?
Also, why doesn’t nodejs just support fetch out of the box?? Feels like a missed opportunity.
Thanks for any tips!
---
*PS: If you’ve got a favorite alternative to fetch in nodejs, lmk!*
Yeah, nodejs doesn’t include fetch by default, which is super annoying. The easiest fix is to install node-fetch—it’s lightweight and works just like the browser version.
Just run:
`npm install node-fetch`
Then in your code:
`const fetch = require('node-fetch');`
If you’re using ES modules, you’ll need to tweak it a bit. Also, axios is another solid option if you want more features.
Kinda weird nodejs hasn’t added fetch in nodejs natively yet, but hey, at least there are workarounds!
Wait, really? I thought fetch in nodejs was a thing now! Guess not.
I’ve been using axios forever—it’s way more reliable for API calls in nodejs IMO. The syntax is clean, and it handles errors better.
Try:
`npm install axios`
Then:
`const axios = require('axios');`
Bonus: it works in browsers too, so you can reuse code.
Lol, welcome to the club. Nodejs and fetch have a weird relationship.
Quick fix: use `undici`—it’s actually the modern replacement for node-fetch and is way faster. Nodejs team recommends it now.
Install:
`npm install undici`
Then:
`import { fetch } from 'undici';`
It’s a bit more low-level, but if speed matters, it’s worth it.
Ugh, this tripped me up too. If you’re on Node 18+, fetch in nodejs *is* actually supported… but only behind a flag or in experimental mode.
Try running your script with:
`node --experimental-fetch yourfile.js`
Otherwise, yeah, node-fetch or axios are the go-tos.
Fun fact: the nodejs team is working on native fetch support, so maybe soon we won’t need workarounds!
Honestly, just use axios. Fetch in nodejs is a pain, and axios has way better error handling and interceptors.
Plus, you can do stuff like this:
```
axios.get('https://api.example.com')
.then(response => console.log(response.data))
.catch(error => console.error('Oops:', error));
```
Way cleaner than dealing with fetch’s quirks.
Pro tip: if you’re using a newer version of nodejs (18+), you can enable fetch globally by using:
`globalThis.fetch = require('node-fetch');`
But yeah, it’s wild that fetch in nodejs isn’t just built-in. For now, node-fetch or undici are your best bets.
Also, check out the docs for undici—it’s the future of fetch in nodejs, apparently.
---
Oh wow, thanks for all the suggestions! Didn’t realize there were so many options.
I tried node-fetch and it worked instantly, so that’s a win. But now I’m curious about undici—everyone’s saying it’s faster. Might give that a shot next.
Also, good to know Node 18+ has experimental fetch support. I’m on 16, so that explains things.
Thanks again, y’all saved me hours of headache!