How to use fetch in Node.js? Getting 'fetch is not defined' error Alternatively, if you want something more

12 Replies, 1217 Views

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!

Messages In This Thread



Users browsing this thread: 1 Guest(s)