Requisition HTTP Client
Requisition is a lightweight, fluent HTTP client designed specifically for Node.js, with a strong emphasis on ES6/ES7 `async/await` patterns. It provides a minimal API for making HTTP requests (GET, POST, etc.) and offers utilities for handling responses, such as parsing JSON, reading as text or buffer, and saving to a file. Unlike browser-compatible alternatives like Axios, Requisition focuses solely on the Node.js environment, foregoing large options objects in favor of a chainable, method-based interface. The package is currently at version 1.7.0, with its last update nearly seven years ago (June 2017), indicating it is no longer actively maintained and should be considered abandoned. Due to its age, it targets older Node.js versions and exclusively uses CommonJS modules.
Common errors
-
ERR_REQUIRE_ESM
cause Attempting to `import requisition` in an ES module context.fixChange `import req from 'requisition';` to `const req = require('requisition');`. -
TypeError: (0 , requisition__WEBPACK_IMPORTED_MODULE_0__.default) is not a function
cause This error occurs in a bundler environment (like Webpack) when `requisition` (a CJS module) is imported as a default ESM import, and the bundler tries to resolve a non-existent default export.fixEnsure your bundler is configured to correctly handle CommonJS modules, or preferably, use `require('requisition')` if possible. Best to migrate to a modern, ESM-compatible HTTP client. -
Error: socket hang up
cause The remote server closed the connection prematurely, often due to a server-side error, proxy issues, or the request taking too long without proper timeout handling.fixIncrease the request timeout using `.timeout(ms)` or investigate server-side logs for the remote service. Check for network connectivity or proxy configuration issues. Ensure the server is not closing the connection due to malformed requests. -
Error: connect ECONNREFUSED
cause The client could not establish a connection to the server, typically because the server is not running, is on a different port, or a firewall is blocking the connection.fixVerify that the target server is running and accessible at the specified URL and port. Check firewall settings on both client and server machines.
Warnings
- breaking The package is explicitly CommonJS (CJS) only, with its last update in June 2017. It does not support ES modules (ESM) natively, and attempting `import requisition` will result in `ERR_REQUIRE_ESM` errors in modern Node.js projects configured for ESM.
- gotcha Requisition is an abandoned package, last updated almost seven years ago. This means it lacks support for modern Node.js features, HTTP/2 or HTTP/3, and may contain unpatched security vulnerabilities or rely on outdated internal dependencies.
- gotcha Error handling with `async/await` in older Node.js versions (which this package targets) might be less robust for unhandled promise rejections compared to modern environments. Always wrap `await` calls in `try...catch` blocks.
- gotcha The `.cookie()` method for setting cookies uses `cookie.serialize()`, but the response's `.cookies` property provides parsed cookies. Manual handling of cookie string parsing/serializing might be needed for complex scenarios or specific server requirements.
Install
-
npm install requisition -
yarn add requisition -
pnpm add requisition
Imports
- req
import req from 'requisition';
const req = require('requisition'); - request
import { request } from 'requisition';const request = require('requisition'); - Request
import { Request } from 'requisition';const Request = require('requisition'); // Request is the function itself, not a class or named export
Quickstart
const req = require('requisition');
async function fetchAndSaveUser(userId) {
try {
console.log(`Fetching user ${userId}...`);
const userResponse = await req(`/users/${userId}.json`);
if (userResponse.status !== 200) {
console.error(`Error fetching user ${userId}: Status ${userResponse.status}`);
await userResponse.dump(); // Consume unhandled body to prevent memory leaks
return;
}
const userData = await userResponse.json();
console.log(`User ${userId} data:`, userData);
// Example: Save an image if the user had one (hypothetical)
if (userData.profileImage) {
console.log(`Fetching profile image for user ${userId}...`);
const imageResponse = await req(userData.profileImage);
if (imageResponse.status === 200) {
const savedPath = await imageResponse.saveTo(`/tmp/user_${userId}_profile.png`);
console.log(`Profile image saved to: ${savedPath}`);
} else {
console.warn(`Could not fetch profile image for user ${userId}: Status ${imageResponse.status}`);
await imageResponse.dump();
}
}
} catch (error) {
console.error('An error occurred during the request:', error.message);
}
}
// To run this example, you'd need a simple local server
// For demonstration purposes, assume '/users/123.json' and '/users/123/image.png' exist
// Example usage (in an async IIFE or main function):
(async () => {
await fetchAndSaveUser(123);
await fetchAndSaveUser(456);
})();