Unirest HTTP Client for Node.js
Unirest for Node.js is a lightweight HTTP client library designed to simplify common HTTP request patterns, including GET, POST, PUT, and file uploads. It offers features like automatic gzip decompression and response parsing, aiming to provide a concise, fluent API. The library's development has been dormant for approximately seven years, with its last stable version (0.11.0) published in 2017. Consequently, it lacks ongoing maintenance and a current release cadence. While it was once part of a multi-language HTTP library suite and associated with Kong, it is now largely superseded by more modern and actively maintained alternatives in the Node.js ecosystem, such as `axios` or `node-fetch`. Developers should be aware of its unmaintained status and potential compatibility issues with newer Node.js versions.
Common errors
-
ReferenceError: require is not defined
cause Attempting to use the CommonJS `require()` function in a file or project configured as an ES module.fixEnsure your file is a CommonJS module (`.js` extension, no `"type": "module"` in `package.json`, or explicitly using a CJS loader). If in an ESM project, use a transpiler to handle `require` or, ideally, migrate to a modern HTTP client. -
TypeError: unirest.post(...).then is not a function
cause Attempting to use Promise-based `.then()` chaining with `unirest` version 0.6.0, which only supports callback-based asynchronous operations.fixRefactor your request to use the callback pattern with the `.end()` method, for example: `unirest.post(...).end(function(response){...});`. Promise support was added in Unirest v0.9.0. -
Error: Cannot find module 'unirest'
cause The `unirest` package was not installed in the project, or Node.js cannot resolve the module path.fixEnsure the package is installed by running `npm install unirest` in your project's root directory. Verify that `node_modules` is present and accessible, and `unirest` is listed in `package.json`.
Warnings
- breaking The `unirest` library has been abandoned since approximately 2017 and is no longer maintained. This creates significant security risks as any discovered vulnerabilities will not be patched, and it lacks updates for new features, bug fixes, or compatibility with newer Node.js versions.
- breaking Version 0.6.0 of `unirest` does not support Promises (e.g., `.then()`, `async/await`) for asynchronous operations. Its API is strictly callback-based, requiring the `.end(callback)` method to send requests and handle responses. The README excerpt incorrectly suggests Promise support for this version.
- gotcha Being an older library, `unirest` (v0.6.0) is a CommonJS-only package. Attempting to import it using ES module `import` syntax (`import unirest from 'unirest';`) in an ES module context will result in runtime errors like `ReferenceError: require is not defined`.
- gotcha The `unirest` package does not ship with official TypeScript definition files. Using it in a TypeScript project for version 0.6.0 will lead to type errors unless manual type declarations are provided or an external `@types/unirest` package is used (which is likely unmaintained).
Install
-
npm install unirest -
yarn add unirest -
pnpm add unirest
Imports
- unirest
import unirest from 'unirest';
const unirest = require('unirest'); - Request (chained methods)
unirest.post(...).then(response => ...);
unirest.post('http://example.com').headers({...}).send({...}).end(callback); - TypeScript types
import type { Unirest } from 'unirest';(No native types)
Quickstart
const unirest = require('unirest');
// Example POST request using the callback-based API (typical for v0.6.0)
unirest.post('http://mockbin.com/request')
.headers({'Accept': 'application/json', 'Content-Type': 'application/json'})
.send({ "parameter": 23, "foo": "bar" })
.end(function (response) {
if (response.error) {
console.error('Request failed:', response.error);
// Log detailed error information for debugging
if (response.error.body) console.error('Error Body:', response.error.body);
if (response.error.status) console.error('Error Status:', response.error.status);
} else {
console.log('Request successful (callback style)!');
console.log('Status:', response.status);
console.log('Body:', response.body);
}
});
// Example GET request with direct callback argument (another common pattern in v0.6.0)
unirest.get('http://mockbin.com/status/200', function (response) {
if (response.error) {
console.error('GET Request failed:', response.error);
} else {
console.log('GET request successful (direct callback)!');
console.log('Status:', response.status);
}
});