http-cookie-agent is a Node.js library that provides HTTP(S) agents capable of managing cookies across various popular HTTP clients, including Node.js global fetch (via undici), undici directly, axios, node-fetch, and the native `node:http`/`node:https` modules. Its current stable version is 7.0.3, with minor bug fix releases occurring relatively frequently and major versions released annually or as significant changes warrant. The library primarily differentiates itself by offering a unified cookie management solution that integrates seamlessly with existing `tough-cookie` instances and supports a wide array of HTTP client libraries, allowing developers to centralize cookie handling in complex Node.js applications without reimplementing logic for each client. It requires `tough-cookie` and, for `fetch` integration, `undici` as peer dependencies.
npm install http-cookie-agentVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to initialize `CookieJar` and `CookieAgent` for `undici` (or Node.js global `fetch`), making two requests to `httpbin.org` to set and then retrieve a cookie.
Refer to the `MIGRATION.md` guide on the `http-cookie-agent` GitHub repository to understand the necessary code adjustments for v7.x.
Ensure `tough-cookie` is installed (`npm install tough-cookie`). For `fetch` or `undici` usage, also install `undici` (`npm install undici`). Check the `http-cookie-agent` README for recommended `undici` versions based on your Node.js version.
This library is designed for Node.js environments. For Bun or Deno, consider their native cookie handling capabilities or alternative libraries designed for those runtimes.
Remove the `async_UNSTABLE` option from your `CookieAgent` configuration. Review alternative asynchronous cookie handling strategies if this feature was critical to your use case.
Install the correct `undici` version matching your Node.js environment as specified in the `http-cookie-agent` documentation (e.g., `npm install undici@7`).
Upgrade your Node.js environment to version 20.0.0 or newer. If you need to support older Node.js versions, consider using an older major version of `http-cookie-agent` that supports your target Node.js runtime, if available.
Run `npm install tough-cookie` in your project directory.
Run `npm install undici` and ensure you are on a compatible Node.js version (>=20.0.0). If you're on Node.js 20+, `fetch` should be global once `undici` is installed, or you can `import { fetch } from 'undici';`.Ensure your project is configured for ESM (e.g., `"type": "module"` in `package.json`) and use `import` statements. Verify the correct import path, such as `import { CookieAgent } from 'http-cookie-agent/undici';`.Ensure you are passing an instance of `CookieAgent` to the `dispatcher` option, e.g., `new CookieAgent({ cookies: { jar } })`. Do not pass the class itself or an uninitialized object.