HTTP client for JavaScript (Node.js): top libraries for working with the network
In modern web development with Node.js, sending HTTP requests is one of the most frequent operations. Whether it's accessing a REST API, downloading files, or interacting with microservices, a reliable HTTP client is essential. In this article, we will take a detailed look at four best libraries for JavaScript (Node.js): Axios, Got, node-fetch, and undici. You will learn their features, performance, and get ready-to-use code examples.
1. Axios — the universal standard
Axios is the most popular HTTP client for Node.js and browsers. It is Promise-based and supports interceptors, automatic JSON parsing, CSRF protection, and request cancellation.
Installation:
npm install axiosGET request example:
const axios = require('axios');
async function getUser(id) { try { const response = await axios.get(`https://api.example.com/users/${id}`); console.log(response.data); } catch (error) { console.error('Error:', error.message); }}
getUser(1);POST example with headers:
const axios = require('axios');
const data = { name: 'Alice', role: 'developer' };
axios.post('https://api.example.com/users', data, { headers: { 'Content-Type': 'application/json' }}).then(res => console.log(res.status));Pros: simplicity, extensive ecosystem, upload progress support.
Cons: package size (~100 KB), overkill for simple tasks.
2. Got — fast and feature-rich
Got is a lightweight HTTP client with a rich API. It supports streaming, caching, retries, and is faster than Axios.
Installation:
npm install gotExample with streaming and error handling:
const got = require('got');const fs = require('fs');
async function downloadFile(url, outputPath) { try { const stream = got.stream(url); const fileStream = fs.createWriteStream(outputPath); stream.pipe(fileStream);
await new Promise((resolve, reject) => { stream.on('end', resolve); stream.on('error', reject); }); console.log('File downloaded'); } catch (error) { console.error('Download error:', error.message); }}
downloadFile('https://example.com/file.zip', './file.zip');Example with retries:
const got = require('got');
got('https://api.example.com/data', { retry: { limit: 3, methods: ['GET'] }}).then(response => console.log(response.body));Pros: small size, speed, built-in retries.
Cons: less intuitive API for beginners.
3. node-fetch — familiar browser syntax
node-fetch is a port of the browser Fetch API for Node.js. It's ideal if you're used to fetch in the browser and want consistent code.
Installation:
npm install node-fetchExample with async/await:
const fetch = require('node-fetch');
async function getPosts() { const response = await fetch('https://jsonplaceholder.typicode.com/posts/1'); if (!response.ok) throw new Error('HTTP error ' + response.status); const data = await response.json(); console.log(data.title);}
getPosts();Example of sending FormData:
const fetch = require('node-fetch');const FormData = require('form-data');
const form = new FormData();form.append('file', fs.createReadStream('./photo.jpg'));\