Intermediate LevelNode JS
How does the native global fetch API in Node.js 18+ replace axios and node-fetch?
❓ Interview Prompt: Node 18+ includes WHATWG standard globalThis.fetch, Headers, Request, Response, and FormData natively powered by the high-performance undici HTTP/1.1 and HTTP/2 client engine. Zero external dependencies needed.
Node.js 18+ includes the native WHATWG 'fetch' API built directly into global scope ('globalThis.fetch'):
Features:
•
Powered by Undici, Node's high-performance HTTP/1.1 and HTTP/2 client.
•
Zero external dependencies required (replaces legacy 'axios' and 'node-fetch' packages)!
Code Implementation Example
javascript
async function fetchGithubUser(username) {
const response = await fetch('https://api.github.com/users/${username}', {
headers: { 'User-Agent': 'Node.js-App' }
});
if (!response.ok) {
throw new Error('HTTP error! status: ${response.status}');
}
const data = await response.json();
return data;
}💡Fetching HTTP API data using native Node.js global fetch.
🎯 Interview Key Takeaway
Use native global fetch in Node 18+ to eliminate external HTTP request client dependencies.
#fetch#WHATWG#Undici#HTTP Client#Intermediate
Back to all Node JS Questions→