HTTP client for JavaScript (Node.js): top libraries for working with the network
Modern development with Node.js rarely goes without interaction with external services. Whether it's a REST API, GraphQL endpoints, or data streaming — you will need a reliable HTTP client. In this article, we will review the best libraries for making HTTP requests in the Node.js environment, their features, and usage examples.
1. Axios — a universal choice
Axios is the most popular HTTP client for Node.js and browsers. It is promise-based, supports interceptors, automatic JSON transformation, and request cancellation.
Installation
npm install axiosGET request example
const axios = require('axios');
async function getUsers() { try { const response = await axios.get('https://jsonplaceholder.typicode.com/users'); console.log(response.data); } catch (error) { console.error('Error:', error.message); }}
getUsers();Features:
- Support for
CancelTokenfor request cancellation - Automatic handling of HTTP errors (4xx and 5xx statuses)
- Works both on the server and in the browser
- Support for upload/download progress
2. Got — performance and flexibility
Got is a lightweight and fast library created specifically for Node.js. It offers a rich API and excellent performance by using the http and https modules directly.
Installation
npm install gotPOST request example
const got = require('got');
async function createPost() { try { const response = await got.post('https://jsonplaceholder.typicode.com/posts', { json: { title: 'New post', body: 'Post content', userId: 1 }, responseType: 'json' }); console.log(response.body); } catch (error) { console.error('Error:', error.message); }}
createPost();Features:
- Support for streams and pagination
- Built-in retry support
- Working with cookies and caching
- Support for HTTP/2 (via
http2-wrapper)
3. Node.js Fetch API — native standard
Starting from Node.js 18, the Fetch API has become a built-in module (available globally). This is an implementation of the same API as in browsers, which simplifies universal code.
Usage example
async function fetchData() { try { 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); } catch (error) { console.error('Error:', error.message); }}
fetchData();Features:
- No installation required — built into Node.js 18+
- Standard API familiar to frontend developers
- Support for
AbortControllerfor request cancellation - Limited capabilities compared to Axios/Got (e.g., no interceptors)
4. undici — high-performance HTTP/1.1 client
undici is a fast, modern HTTP client that underlies the built-in Fetch in Node.js. It is optimized for maximum throughput.
Installation
npm install undiciRequest example
const { request } = require('undici');
async function fetchWithUndici() { try { const { statusCode, body } = await request('https://jsonplaceholder.typicode.com/users'); const data = await body.json(); console.log('Status:', statusCode); console.log('Da