{"id":17232,"library":"forever-agent","title":"HTTP Keep-Alive Agent","description":"forever-agent is a Node.js module that provides an HTTP Agent designed to maintain persistent socket connections between `http.request` calls, enabling HTTP keep-alive behavior. It was originally an internal component of the widely popular, but now deprecated, `request` HTTP client library. The current stable version, 0.6.1, was last published over 11 years ago in April 2015. With the official deprecation of its progenitor `request` library in 2020, forever-agent itself is considered unmaintained and has seen no significant updates for over a decade. Its key differentiator was offering direct control over connection pooling for keep-alive outside of the `request` library, at a time when native Node.js HTTP client features were less developed regarding persistent connections.","status":"abandoned","version":"0.6.1","language":"javascript","source_language":"en","source_url":"https://github.com/mikeal/forever-agent","tags":["javascript"],"install":[{"cmd":"npm install forever-agent","lang":"bash","label":"npm"},{"cmd":"yarn add forever-agent","lang":"bash","label":"yarn"},{"cmd":"pnpm add forever-agent","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package is a CommonJS module from an older era. ESM `import` syntax will not work directly and attempting to use it may result in `ERR_REQUIRE_ESM`.","wrong":"import { ForeverAgent } from 'forever-agent';","symbol":"ForeverAgent","correct":"const ForeverAgent = require('forever-agent');"},{"note":"ForeverAgent is a constructor function and must be instantiated with the `new` keyword to create an agent instance.","wrong":"const agent = ForeverAgent();","symbol":"HTTP Agent Instance","correct":"const agent = new ForeverAgent();"}],"quickstart":{"code":"const http = require('http');\nconst ForeverAgent = require('forever-agent');\n\nconst agent = new ForeverAgent();\n\n// Define request options, ensuring 'Connection: keep-alive' is set.\nconst options = {\n  hostname: 'httpbin.org', // A public service for testing HTTP requests\n  port: 80,\n  path: '/get',\n  method: 'GET',\n  agent: agent, // Crucially, assign the forever-agent instance\n  headers: {\n    'Connection': 'keep-alive'\n  }\n};\n\nfunction makeKeepAliveRequest(id) {\n  return new Promise((resolve, reject) => {\n    console.log(`[Request ${id}] Sending request...`);\n    const req = http.request(options, (res) => {\n      let data = '';\n      res.on('data', (chunk) => { data += chunk; });\n      res.on('end', () => {\n        console.log(`[Request ${id}] Status: ${res.statusCode}, Socket Local Port: ${req.socket ? req.socket.localPort : 'N/A'}`);\n        resolve(res.statusCode);\n      });\n    });\n\n    req.on('error', (e) => {\n      console.error(`[Request ${id}] Problem with request: ${e.message}`);\n      reject(e);\n    });\n\n    req.end();\n  });\n}\n\nasync function demonstrateKeepAlive() {\n  console.log('Demonstrating HTTP keep-alive with forever-agent...');\n  await makeKeepAliveRequest(1);\n  await makeKeepAliveRequest(2);\n  await makeKeepAliveRequest(3);\n  console.log('Multiple requests sent. Check console for repeated socket local ports to confirm keep-alive.');\n}\n\ndemonstrateKeepAlive();","lang":"javascript","description":"This quickstart demonstrates how to instantiate `ForeverAgent` and use it with Node.js's native `http.request` to achieve persistent (keep-alive) connections across multiple requests to the same host."},"warnings":[{"fix":"Migrate to `undici` (Node.js 18+) or use the native `http.Agent`/`https.Agent` with `keepAlive: true` and `maxSockets` options for connection pooling. For example:\n`const agent = new http.Agent({ keepAlive: true, maxSockets: 10 });`","message":"The `forever-agent` package is effectively abandoned, with its last publish over 11 years ago (April 2015). It was originally part of the `request` library, which itself was officially deprecated in 2020. Consider using built-in Node.js `http`/`https` agents with `keepAlive` options or modern, actively maintained HTTP client libraries like `axios`, `node-fetch`, or `undici` for better performance, security, and feature sets.","severity":"deprecated","affected_versions":">=0.6.1"},{"fix":"Update to an actively maintained HTTP client library or utilize Node.js's native `Agent` options for `keepAlive` functionality. Thoroughly test existing applications if upgrading Node.js versions while still relying on `forever-agent`.","message":"Due to its age and lack of maintenance, `forever-agent` may not be compatible with newer Node.js versions, particularly regarding internal `http` module changes, TLS/SSL updates, or event loop behavior. Unexpected errors, memory leaks, or incorrect connection handling could occur.","severity":"breaking","affected_versions":">=0.6.1"},{"fix":"Immediately replace `forever-agent` with a modern, actively maintained HTTP client library or the native `http.Agent`/`https.Agent` which receive regular security patches as part of Node.js core.","message":"This package has not received security updates for over a decade. It may contain unfixed vulnerabilities, especially concerning parsing HTTP headers, handling malformed responses, or TLS handshake issues, making it unsuitable for production environments where security is critical.","severity":"gotcha","affected_versions":">=0.6.1"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Use CommonJS `require` syntax instead: `const ForeverAgent = require('forever-agent');`","cause":"Attempting to import `forever-agent` using ES Module `import` syntax in a JavaScript module that is treated as an ESM module (e.g., in a package with `\"type\": \"module\"` or a `.mjs` file).","error":"ERR_REQUIRE_ESM"},{"fix":"Ensure you are instantiating it as a class: `const agent = new ForeverAgent();`","cause":"Attempting to call `ForeverAgent` as a function without the `new` keyword, or a bundling/transpilation issue incorrectly exporting/importing it.","error":"TypeError: ForeverAgent is not a constructor"},{"fix":"This is a warning, not an error. The fix is to acknowledge the deprecation and plan for migration to a modern, supported alternative like `undici` or the native Node.js `http.Agent` with `keepAlive`.","cause":"This is a warning from `npm install` indicating that the package is no longer actively developed and may have security or compatibility issues.","error":"NPM WARN deprecated forever-agent@0.6.1: This package is very old and unmaintained."}],"ecosystem":"npm","meta_description":null}