I'm learning JS on the side and every tutorial I find uses axios, but people keep telling me fetch is built in now. For a small Node script plus a simple frontend, is there any reason to install axios? And what am I missing if I just use fetch? π
Questionfetch vs axios in 2026 β do I still need axios?
For a small project, fetch is enough. It's global in browsers and in Node (Node's fetch is built on Undici; Node 26 ships Undici 8: https://nodejs.org/en/blog/release/v26.0.0). One less dependency is also one less package in your supply chain.
What you need to know, because fetch behaves differently from axios:
1. fetch does not throw on 404 or 500. It only rejects on network errors. Check res.ok yourself.
2. No timeout by default. Use AbortSignal.timeout().
3. You parse the body yourself: res.json(), res.text().
A small wrapper covers all three:
export async function getJson(url, { timeoutMs = 5000, ...init } = {}) {
const res = await fetch(url, { ...init, signal: AbortSignal.timeout(timeoutMs) });
if (!res.ok) {
const body = await res.text().catch(() => '');
throw new Error(`HTTP ${res.status} ${res.statusText}: ${body.slice(0, 200)}`);
}
return res.json();
}
export async function postJson(url, data, opts = {}) {
return getJson(url, {
...opts,
method: 'POST',
headers: { 'content-type': 'application/json', ...opts.headers },
body: JSON.stringify(data),
});
}
On timeout the promise rejects with a TimeoutError (check err.name).
Where axios still earns its place, so it's a fair comparison:
- interceptors (attach a token to every request, refresh it on 401) β with fetch you write that in your own wrapper;
- upload progress in the browser (axios uses XHR there; fetch has no simple upload progress);
- a large existing codebase that already uses it everywhere.
If none of these apply, I wouldn't add it to a new project.
Question from a beginner too:
1. Does the same getJson work in the browser without changes?
2. What about CORS errors, is that something fetch does and axios does not?
>>4
1. Yes, fetch, AbortSignal.timeout and res.json() are the same API in current browsers.
2. CORS is enforced by the browser, not by the library. axios hits exactly the same error. The fix is on the server that you call: it must send Access-Control-Allow-Origin for your site. If it's not your server, call it from your own backend instead of the browser. Turning CORS "off" with browser extensions only hides the problem on your machine.
Got it, fetch + a tiny wrapper it is. The res.ok thing would have confused me for days, thanks for spelling it out π
Only signed-in members can write. Sign in