HTTP Cookie Agent
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.
Common errors
-
Error: Cannot find package 'tough-cookie' imported from ...
cause The `tough-cookie` peer dependency is not installed.fixRun `npm install tough-cookie` in your project directory. -
TypeError: fetch is not a function
cause This usually indicates that `undici` is not installed, or the Node.js version being used does not natively provide global `fetch` without `undici`.fixRun `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';`. -
TypeError: CookieAgent is not a constructor
cause Attempting to `require` the `CookieAgent` or other ESM-only exports in a CommonJS context, or using an incorrect import path.fixEnsure 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';`. -
Error: `dispatcher` must be an instance of `Dispatcher`.
cause When using `undici` or Node.js global fetch, the `dispatcher` option expects an instance of an `undici` dispatcher. This error often arises if `CookieAgent` is not correctly instantiated or passed.fixEnsure 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.
Warnings
- breaking Version 7.0.0 introduced breaking changes. Users upgrading from v6 should consult the `MIGRATION.md` file in the GitHub repository for detailed instructions.
- gotcha This package has peer dependencies (`tough-cookie` and `undici`) that must be installed separately. Failing to install them will result in runtime errors.
- gotcha `http-cookie-agent` explicitly does not support Bun's and Deno's proprietary `fetch` implementations due to their differing internal structures.
- breaking The `async_UNSTABLE` option was removed in version 6.0.4 due to incorrect functionality. Code relying on this option will no longer work.
- gotcha Node.js global `fetch` support relies on `undici`. The required `undici` version might vary depending on your Node.js version (e.g., `undici@6` for Node.js 20/22/23, `undici@7` for Node.js 24).
- gotcha This library requires Node.js version 20.0.0 or higher. Running on older Node.js versions will lead to compatibility issues or errors.
Install
-
npm install http-cookie-agent -
yarn add http-cookie-agent -
pnpm add http-cookie-agent
Imports
- CookieJar
const { CookieJar } = require('tough-cookie');import { CookieJar } from 'tough-cookie'; - CookieAgent
const { CookieAgent } = require('http-cookie-agent/undici'); import { CookieAgent } from 'http-cookie-agent';import { CookieAgent } from 'http-cookie-agent/undici'; - HttpsCookieAgent
import { CookieAgent } from 'http-cookie-agent/http';import { HttpsCookieAgent } from 'http-cookie-agent/http'; - cookie (undici interceptor)
import { CookieAgent } from 'http-cookie-agent/undici';import { cookie } from 'http-cookie-agent/undici';
Quickstart
import { CookieJar } from 'tough-cookie';
import { CookieAgent } from 'http-cookie-agent/undici';
import { fetch } from 'undici'; // Or use global fetch if undici is installed for Node.js >=20
async function makeRequestWithCookies() {
const jar = new CookieJar();
const agent = new CookieAgent({ cookies: { jar } });
console.log('Sending first request to set a cookie...');
const firstResponse = await fetch('https://httpbin.org/cookies/set?mycookie=myvalue', {
dispatcher: agent
});
console.log(`First request status: ${firstResponse.status}`);
await firstResponse.text(); // Consume body to ensure cookies are processed
console.log('Sending second request to retrieve the cookie...');
const secondResponse = await fetch('https://httpbin.org/cookies', {
dispatcher: agent
});
console.log(`Second request status: ${secondResponse.status}`);
const cookieData = await secondResponse.json();
console.log('Cookies received in second request:', cookieData.cookies);
if (cookieData.cookies.mycookie === 'myvalue') {
console.log('Success: Cookie was correctly sent and received!');
} else {
console.log('Failure: Cookie was not handled as expected.');
}
}
makeRequestWithCookies().catch(console.error);