timed-out HTTP/HTTPS Request Timeout
The `timed-out` package provides a simple and effective mechanism for adding timeout functionality to Node.js `http.ClientRequest` objects, preventing requests from hanging indefinitely. It automatically emits an `Error` object with specific `code` properties (`ETIMEDOUT` or `ESOCKETTIMEDOUT`) when a request exceeds its defined time limit. The current stable version is `7.0.0`. The project maintains a steady release cadence, typically introducing new major versions to align with Node.js LTS releases and introduce breaking changes like pure ESM adoption. Its key differentiator lies in its focused approach: it specifically extends the native `ClientRequest` object, offering granular control over connection and socket activity timeouts, rather than being part of a larger HTTP client library. This makes it a lightweight solution for augmenting existing `http` and `https` module usage without introducing a full-fledged client.
Common errors
-
Error [ERR_REQUIRE_ESM]: require() of ES Module .../node_modules/timed-out/index.js from ... not supported.
cause Attempting to import `timed-out` using `require()` in a CommonJS context after the package converted to pure ESM in v6.0.0.fixConvert your project or file to ES Modules by adding `"type": "module"` to your `package.json` or by using `.mjs` file extensions, and then use `import timedOut from 'timed-out';`. If CommonJS is unavoidable, use `timed-out@<6.0.0`. -
TypeError: timedOut is not a function
cause This error typically occurs when attempting to import a default export as a named export (e.g., `import { timedOut } from 'timed-out';`) or when the import is incorrect after the ESM transition.fixEnsure you are using the correct default import syntax for ES Modules: `import timedOut from 'timed-out';`. -
TypeError: The "request" argument must be an instance of ClientRequest. Received type <...>
cause The first argument passed to the `timedOut` function is not a valid Node.js `http.ClientRequest` object, which is expected by the package.fixEnsure you are passing an actual `ClientRequest` instance, typically obtained from `http.get()`, `http.request()`, `https.get()`, or `https.request()`. -
Error: Request timed out after X milliseconds (or ETIMEDOUT / ESOCKETTIMEDOUT code)
cause The HTTP/HTTPS request exceeded the specified `time` limit, either during connection (`connect` timeout) or while waiting for activity on the socket (`socket` timeout). This is often an expected runtime condition rather than a code error.fixImplement robust error handling for `ETIMEDOUT` or `ESOCKETTIMEDOUT` error codes. Consider increasing the timeout duration if the network or server is genuinely slow, or implement retry logic and user notifications.
Warnings
- breaking Version 7.0.0 requires Node.js 20 or later. Earlier Node.js versions are not supported.
- breaking Version 6.0.0 converted the package to pure ESM (ECMAScript Modules). It is no longer compatible with CommonJS `require()` syntax.
- breaking Version 6.0.0 requires Node.js 12 or later. Previous versions of Node.js are no longer supported.
- breaking Version 5.0.0 requires Node.js 8 or later. Running on earlier Node.js versions will cause issues.
Install
-
npm install timed-out -
yarn add timed-out -
pnpm add timed-out
Imports
- timedOut
const timedOut = require('timed-out');import timedOut from 'timed-out';
- timedOut
import { timedOut } from 'timed-out';import timedOut from 'timed-out';
Quickstart
import http from 'node:http';
import timedOut from 'timed-out';
// Example 1: Basic timeout for a GET request
const request1 = http.get('http://www.google.com', (res) => {
console.log('Request 1 response status:', res.statusCode);
res.resume(); // Consume response data to prevent memory leaks
});
timedOut(request1, 2000); // Sets a 2-second timeout
request1.on('timeout', () => {
request1.destroy(new Error('Request 1 timed out after 2 seconds'));
console.error('Request 1 timed out!');
});
request1.on('error', (err) => {
if (err.code === 'ETIMEDOUT' || err.code === 'ESOCKETTIMEDOUT') {
console.error('Request 1 failed due to timeout:', err.message);
} else {
console.error('Request 1 encountered an error:', err.message);
}
});
// Example 2: Granular connect and socket timeouts for a different request
const request2 = http.get('http://www.example.com', (res) => {
console.log('Request 2 response status:', res.statusCode);
res.resume();
});
timedOut(request2, { connect: 1000, socket: 3000 }); // Connect timeout 1s, socket activity timeout 3s
request2.on('timeout', () => {
request2.destroy(new Error('Request 2 timed out'));
console.error('Request 2 timed out!');
});
request2.on('error', (err) => {
if (err.code === 'ETIMEDOUT' || err.code === 'ESOCKETTIMEDOUT') {
console.error('Request 2 failed due to timeout:', err.message);
} else {
console.error('Request 2 encountered an error:', err.message);
}
});
// In a real application, ensure all requests are explicitly handled or destroyed
// to prevent lingering connections or memory leaks. For testing timeouts,
// you might need to use a slow or non-existent endpoint.