![]() |
|
[b]"Why isn't fetch in Node.js working like it does in the browser?"[/b]
or
[b]"What's the best way to use fetch i - Printable Version +- Proxy Community (https://proxycommunity.com/forum) +-- Forum: Technical Community Support (https://proxycommunity.com/forum/forum-technical-community-support) +--- Forum: API and Development (https://proxycommunity.com/forum/forum-api-and-development) +--- Thread: [b]"Why isn't fetch in Node.js working like it does in the browser?"[/b] or [b]"What's the best way to use fetch i (/thread-b-why-isn-t-fetch-in-node-js-working-like-it-does-in-the-browser-b-%0A%0Aor-%0A%0A-b-what-s-the-best-way-to-use-fetch-i) Pages:
1
2
|
[b]"Why isn't fetch in Node.js working like it does in the browser?"[/b] or [b]"What's the best way to use fetch i - MaskedStormX - 11-12-2024 "Why isn't fetch in nodejs working like it does in the browser?" hey guys, so i’m trying to use fetch in nodejs for a simple API call, but it’s acting weird?? in the browser, it works fine, but in node, i keep getting "fetch is not defined" errors. what gives? i thought node had fetch now? or do i still need to install something? also, if there’s a better way to do this natively, lmk. not sure if i should use axios or stick with fetch in nodejs. thanks for any help! --- *(or if you prefer a shorter version * "Fetch in nodejs keeps giving me errors—what am i missing?" ugh, why is fetch in nodejs so finicky?? works in chrome, breaks in node. do i need a polyfill or something? help a dev out pls 😅 “” - secureFly77 - 30-01-2025 Ah, the classic fetch in nodejs struggle! Node didn’t include fetch by default until recently, so you’ll need to either: - Use Node 18+ (it’s built-in now!) - Install node-fetch or undici if you’re on an older version. Also, check if you’re mixing CommonJS/modules—that can break it. Axios is solid too, but fetch in nodejs is totally viable once you sort the setup. “” - WebPhantom99 - 20-02-2025 yep, fetch in nodejs is kinda new as a native thing. if you’re on node <18, you gotta install node-fetch (or undici, which is what node’s fetch actually uses under the hood). pro tip: if you’re using ESM, make sure to import it right—`import fetch from 'node-fetch'` vs require() can trip you up. axios is more stable for older projects, but fetch is fine if you’re up-to-date! “” - maskedXpert_99 - 19-03-2025 lol welcome to the "why doesn’t fetch in nodejs work" club. 😅 short answer: node only added it natively in v18. before that, you needed node-fetch. even now, some folks prefer undici for perf. if you’re stuck, just `npm install node-fetch` and carry on. or yeah, axios is the OG for a reason—fewer surprises. “” - proxyHorizon99 - 27-03-2025 Fetch in nodejs is weird because it wasn’t a thing until Node 18. Before that, everyone used node-fetch or axios. If you’re getting "fetch is not defined," you’re probably on an older Node version. Check with `node -v`. Quick fix: `npm install node-fetch@2` (v3+ is ESM-only, which might break your setup). Or upgrade Node! “” - MaskedStormX - 30-03-2025 wait, so if i’m on Node 16, i *have* to install node-fetch? that’s annoying. just tried `npm install node-fetch@2` and it worked! but now i’m wondering if i should upgrade to Node 18 instead. also, why’s everyone hyping undici? is it faster or something? thanks for the help, y’all! 🙏 “” - fastAnonyX - 01-04-2025 ugh, fetch in nodejs is such a mess. it’s native in Node 18+, but if you’re not there yet, you’ll need node-fetch. fun fact: node’s built-in fetch uses undici, so you could just install that directly (`npm install undici`) and use its fetch. axios is more bulletproof tho, esp if you need interceptors or older Node support. “” - darkJumpX77 - 01-04-2025 Fetch in nodejs is a recent addition, so if you’re not on Node 18 or later, it won’t work out of the box. Either upgrade Node or install node-fetch. If you go the install route, watch out—node-fetch v3+ requires ESM, so stick with v2 if you’re using CommonJS. Axios is a safe bet if you don’t wanna deal with this mess. “” - DarkOrbit77 - 03-04-2025 ohhhh this is a rite of passage for node devs. fetch in nodejs wasn’t a thing until Node 18, so you’ve got options: 1. Upgrade to Node 18+ (easiest if you can). 2. `npm install node-fetch` (but v3+ needs ESM, so maybe v2). 3. Use undici (`npm install undici`)—it’s the engine behind node’s fetch. axios is chill too, but fetch is legit once you sort the setup. |