Registry / http-networking / get-it

get-it

JSON →
library8.7.2jsnpmunverified

get-it is a generic HTTP request library designed for both Node.js (>=14.0.0) and modern browsers, including web workers. Currently at stable version 8.7.2, the package maintains an active development pace with frequent bug fixes and minor feature releases within its v8.x series. Its key differentiator is a highly modular, middleware-based architecture, drawing inspiration from `http-client`. This approach enables developers to compose specific functionalities such as Promise or Observable patterns, automatic request retries, cancellation, JSON serialization/deserialization, GZIP unwrapping (Node.js), base URL prepending, redirect following, and detailed upload/download progress events. The modularity aims to provide a small browser bundle footprint while offering extensive and configurable HTTP client features. It transparently handles various request body types depending on the execution environment and provides options for network timeouts.

npm install get-it
INSTALL
IMPORT
SIG · GET-IT
G
get-it
http-networkingjavascriptv8.7.2
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

getIt
import { getIt } from 'get-it'
const { getIt } = require('get-it')
get-it primarily uses ESM imports. Direct CommonJS require() may cause issues in some setups.
base, jsonResponse, promise
import { base, jsonResponse, promise } from 'get-it/middleware'
import { base, jsonResponse, promise } from 'get-it'
Middleware functions are imported from the 'get-it/middleware' submodule. Importing directly from 'get-it' will fail.
observable
import { observable } from 'get-it/middleware'
import { Observable } from 'rxjs'
The `observable` middleware provides an RxJS-compatible interface and should be imported from 'get-it/middleware'. It requires an Observable implementation to be passed or globally available.

This quickstart demonstrates how to configure and use `get-it` with common middleware for making asynchronous HTTP requests using Promises, including setting a base URL, parsing JSON responses, and handling basic GET/POST operations and errors.

import { getIt } from 'get-it'; import { base, jsonResponse, promise, timeout } from 'get-it/middleware'; // Create a configured HTTP client instance const httpClient = getIt([ // Base URL middleware prepends 'https://api.example.com/v1' to all request URLs base('https://api.example.com/v1'), // jsonResponse middleware automatically parses JSON responses jsonResponse(), // promise middleware enables the .then/.catch syntax for requests promise(), // timeout middleware sets connection and socket timeouts timeout({ connect: 5000, socket: 10000 }) ]); interface Project { id: string; name: string; status: string; } async function fetchProjects() { try { // Make a GET request to /projects relative to the base URL const response = await httpClient({ url: '/projects', method: 'GET' }); const projects: Project[] = response.body; // body is already parsed by jsonResponse middleware console.log('Fetched projects:', projects.map(p => p.name).join(', ')); // Example of a POST request with JSON body const newProject: Project = await httpClient({ url: '/projects', method: 'POST', body: { name: 'New Awesome Project', status: 'pending' } }).then(res => res.body as Project); console.log('Created project:', newProject.name); } catch (err: any) { console.error('An error occurred:', err.message); if (err.response) { console.error('Response status:', err.response.statusCode); console.error('Response body:', err.response.body); } } } fetchProjects();
Debug
Known issues
breaking`get-it` v8.x requires Node.js version >= 14.0.0. Projects running on older Node.js environments will encounter errors.
fix
Upgrade your Node.js environment to version 14.0.0 or higher. Consider using `nvm` to manage multiple Node.js versions.
affects: >=8.0.0
gotchaBy default, `getIt()` returns a low-level event emitter stream. To use a Promise-based API (e.g., `.then()`, `await`), you must explicitly apply the `promise()` middleware to your `getIt` instance.
fix
Ensure `promise()` middleware is included in your `getIt` configuration: `getIt([/* other middleware */, promise()])` or `request.use(promise())`.
affects: >=1.0.0
gotchaBehavior for certain options like `maxRedirects` and the type of `rawBody` varies between Node.js and browser environments due to underlying platform differences. `maxRedirects` only applies in Node.js, while browsers handle redirects internally. `rawBody` returns `ArrayBuffer` in browsers and `Buffer` in Node.js.
fix
Be mindful of the execution environment when developing universal code. Implement conditional logic or abstraction layers if these differences are critical to your application's behavior.
affects: >=1.0.0
gotchaThe library's middleware architecture means that omitting crucial middleware, such as `jsonResponse()`, will prevent automatic JSON parsing, leading to raw string responses even if the server sends JSON.
fix
Always include `jsonResponse()` middleware if you expect to receive and automatically parse JSON responses. Conversely, omit it if you need the raw string body.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: request.then is not a function
The `promise()` middleware was not applied to the `get-it` instance, meaning the returned object is an event emitter, not a Promise.
fix
Add `promise()` to your middleware chain: `const request = getIt([..., promise()]);`
Error: "Could not parse response body as JSON"
The `jsonResponse()` middleware was used, but the server's response was not valid JSON or the `Content-Type` header did not indicate JSON.
fix
Verify the server is sending valid JSON and setting the `Content-Type` header correctly (e.g., `application/json`). If the response is not JSON, remove `jsonResponse()` middleware or add custom parsing logic.
ERR_REQUIRE_ESM
Attempting to import `get-it` or its middleware using CommonJS `require()` syntax in an environment that expects ESM, or when the package's `package.json` explicitly defines it as type `module`.
fix
Use ESM `import` statements: `import { getIt } from 'get-it';` and `import { ... } from 'get-it/middleware';`. Ensure your project is configured for ESM (e.g., `"type": "module"` in `package.json` or using `.mjs` extensions).
Error: Request timed out after Xms
The HTTP request exceeded the configured `connect` or `socket` timeout, typically due to network latency, server unresponsiveness, or an unreachable host.
fix
Increase the `timeout` values in the `timeout` middleware (e.g., `{ connect: 10000, socket: 30000 }`). Verify network connectivity to the target server.
Upgrade
Version history
8.7.2latest on npm
Audit
Dependencies
follow-redirectsrequiredUsed for automatic HTTP redirect handling in Node.js environments.
@types/follow-redirectsrequiredTypeScript type definitions for follow-redirects, moved to dependencies for broader compatibility.
@types/progress-streamrequiredTypeScript type definitions for progress-stream, moved to dependencies for broader compatibility.
Agent activity
2 hits · last 30 days
node
2
Resources
get-it — npm install get-it · libregistry