Synchronous HTTP Request Utility
This package provides a synchronous HTTP request utility for Node.js, built on top of `urllib` and utilizing Node.js's `spawnSync` module to achieve blocking I/O. The current stable version is 1.1.4, and it has not been updated in approximately nine years, indicating it is no longer maintained. Its primary function is to perform HTTP requests that halt the Node.js event loop until a response is received, a pattern generally considered an anti-pattern in asynchronous Node.js environments. Key differentiators include its explicit synchronous operation, contrasting with the typically non-blocking nature of Node.js. It explicitly states support only for Node.js v0.11.13+ and does not support features like response streams or custom HTTP agents, limiting its utility for modern applications requiring efficient handling of large data or complex network interactions.
Common errors
-
Error: The 'child_process.spawnSync' API is not available on this Node.js version.
cause urllib-sync relies on internal Node.js APIs (like `spawnSync` from `child_process`) that have changed or become incompatible in newer Node.js versions, breaking the package's functionality.fixThis package is incompatible with modern Node.js. Update your application to use asynchronous HTTP clients instead of `urllib-sync`. -
ReferenceError: require is not defined in ES module scope
cause Attempting to use `require('urllib-sync')` in a Node.js environment configured for ECMAScript Modules (ESM) where CommonJS `require` is not directly available.fixurllib-sync is a CommonJS package. Either refactor your project to use CommonJS modules or migrate your HTTP requests to a modern, ESM-compatible asynchronous library like `node-fetch` or native `fetch`. -
TypeError: request is not a function (or similar, if trying to call the module directly)
cause The package exports an object with a `request` method, not the function directly. If you `require('urllib-sync')` and try to call the result, it will fail.fixEnsure you are destructuring the `request` function correctly: `const { request } = require('urllib-sync');` or accessing it as a property: `const request = require('urllib-sync').request;`
Warnings
- breaking This package explicitly supports only Node.js v0.11.13+. It is incompatible with all modern Node.js versions (v12+ onwards) due to significant changes in Node.js internals and API availability.
- gotcha Using synchronous HTTP requests in Node.js will block the entire event loop, making your application unresponsive. This is a severe performance anti-pattern and should be avoided in server-side or long-running applications.
- gotcha The package does not support streams or custom HTTP agents. This limits its use cases, especially for handling large responses efficiently or integrating with proxy/advanced networking setups.
- deprecated The package is effectively abandoned, with its last publish nearly a decade ago and no updates to support modern Node.js versions or fix potential vulnerabilities or bugs. Its underlying approach (synchronous HTTP) is generally deprecated in Node.js best practices.
Install
-
npm install urllib-sync -
yarn add urllib-sync -
pnpm add urllib-sync
Imports
- request
import { request } from 'urllib-sync'; // Error: require is not defined in ES module scopeconst { request } = require('urllib-sync'); - request
const urllibSync = require('urllib-sync'); urllibSync(); // TypeError: urllibSync is not a functionconst request = require('urllib-sync').request;
Quickstart
const { request } = require('urllib-sync');
try {
console.log('Making a synchronous HTTP request...');
// This will block the Node.js event loop until the request completes.
const res = request('https://www.google.com');
console.log(`Status: ${res.status}`);
console.log('Headers:');
for (const key in res.headers) {
console.log(` ${key}: ${res.headers[key]}`);
}
// Note: res.data is a Buffer by default. Convert to string for display.
console.log(`Body snippet: ${res.data.toString().substring(0, 100)}...`);
// Example with options (e.g., method, data, timeout)
// const postData = JSON.stringify({ key: 'value' });
// const postRes = request('https://httpbin.org/post', {
// method: 'POST',
// data: postData,
// headers: { 'Content-Type': 'application/json' },
// timeout: 5000 // In milliseconds
// });
// console.log(`POST Status: ${postRes.status}, Body: ${postRes.data.toString()}`);
} catch (error) {
console.error('Synchronous request failed:', error.message);
}