Registry / http-networking / node-fetch-cookies

node-fetch-cookies

JSON →
library2.1.1jsnpmunverified

node-fetch-cookies is a wrapper around the popular `node-fetch` library, specifically designed to add robust support for HTTP cookie management in Node.js environments. It enables developers to easily handle cookie-jars, allowing for persistent session management across multiple requests. The library supports both in-memory cookie storage and file-based persistence using JSON files, minimizing disk I/O by only saving when explicitly instructed. As of version 2.1.1, the project is actively maintained, addressing recent bug fixes related to redirect handling and cookie attribute parsing. It primarily targets modern Node.js environments (>=14.13.0) and leverages ES Modules for its primary usage patterns. Its key differentiators include simplified cookie persistence and re-exporting core `node-fetch` components, providing a familiar API while adding essential cookie functionality.

npm install node-fetch-cookies
INSTALL
IMPORT
SIG · NODE-FETCH-COOKIES
N
node-fetch-cookies
http-networkingjavascriptv2.1.1
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

fetch
✓ import { fetch } from 'node-fetch-cookies';
✗ import fetch from 'node-fetch-cookies';
The primary `fetch` function with cookie-jar support is a named export, not a default export. Using `require()` might lead to `ERR_REQUIRE_ESM` errors in modern Node.js environments without proper transpilation or configuration, as the library primarily targets ESM.
CookieJar
✓ import { CookieJar } from 'node-fetch-cookies';
✗ const CookieJar = require('node-fetch-cookies').CookieJar;
The `CookieJar` class is a named export. While `require()` might technically work if there's a CJS entry point, the official examples use ESM `import` statements, aligning with Node.js >=14.13.0 engines.
nodeFetch
✓ import { nodeFetch } from 'node-fetch-cookies';
This re-exports the original `fetch` function from `node-fetch` without cookie-jar modifications, useful for direct interaction with `node-fetch`'s core functionality when cookies are not needed or handled manually.
Headers
✓ import { Headers } from 'node-fetch-cookies';
This re-exports the `Headers` class from `node-fetch`, allowing consistent usage of `node-fetch`'s Headers API within a `node-fetch-cookies` context.

Demonstrates initializing a CookieJar with file persistence, loading existing cookies, making multiple requests with automatic cookie handling, and saving updated cookies to disk.

import { fetch, CookieJar } from "node-fetch-cookies"; import { promises as fs } from 'fs'; (async () => { const jarFilePath = "jar.json"; // Create a CookieJar instance const cookieJar = new CookieJar(jarFilePath); // Attempt to load cookies from file if it exists try { await cookieJar.load(); console.log(`Loaded cookies from ${jarFilePath}`); } catch (error) { if (error.code === 'ENOENT') { console.log(`No existing cookie jar found at ${jarFilePath}. Starting fresh.`); } else { console.error(`Error loading cookie jar:`, error); return; } } // Make a request, cookies will be sent/received via the cookieJar console.log("Making initial request to example.com..."); let response = await fetch(cookieJar, "https://example.com"); console.log(`Status: ${response.status}`); console.log("Headers received:", response.headers.raw()); // You can now make another request to a different page on the same domain or a subdomain // and the cookies from the first request will be automatically sent. console.log("Making second request to example.com/path..."); response = await fetch(cookieJar, "https://example.com/path"); console.log(`Status: ${response.status}`); // Save the received cookies to disk await cookieJar.save(); console.log(`Saved cookies to ${jarFilePath}`); })();
Debug
Known issues
breakingOlder versions (prior to v2.1.1) had a `TypeError` when deleting the `content-length` header during redirects that changed methods to `GET`.
fix
Upgrade to version `2.1.1` or newer. If upgrading is not possible, ensure your application does not rely on automatic `content-length` header removal during redirect following.
affects: <2.1.1
gotchaPrior to v2.1.1, duplicate cookie headers could be sent when following redirects, potentially leading to unexpected server behavior or rejection.
fix
Upgrade to version `2.1.1` or newer to ensure correct cookie header handling during redirects.
affects: <2.1.1
breakingVersions prior to v2.1.0 contained incorrect handling of 301, 302, and 303 HTTP redirects, which could lead to incorrect request behavior or lost cookies during redirection chains.
fix
Upgrade to version `2.1.0` or newer for corrected and more robust HTTP redirect handling.
affects: <2.1.0
gotchaThe library primarily uses ES Modules (`import`/`export`) in its documentation and targets Node.js >=14.13.0. While CommonJS (`require()`) might work with bundlers or specific configurations, direct usage in CJS projects without transpilation may lead to `ERR_REQUIRE_ESM` errors.
fix
For new projects, prefer using ESM (`import`). If using in a CommonJS project, ensure your build setup correctly handles ESM dependencies or consider explicitly importing `node-fetch-cookies` using dynamic import (`await import('node-fetch-cookies')`).
affects: >=2.0.0
Errors
Common errors & fixes
TypeError: Attempted to delete 'content-length' header when following redirect and changing method to GET.
A bug in older versions of `node-fetch-cookies` incorrectly tried to modify headers on redirected GET requests.
fix
Upgrade `node-fetch-cookies` to version `2.1.1` or newer.
Duplicate 'cookie' headers are sent on subsequent requests after a redirect.
A bug in older versions of `node-fetch-cookies` caused redundant cookie headers to be included after redirects.
fix
Update `node-fetch-cookies` to version `2.1.1` or higher.
HTTP 301/302/303 redirects are not handled correctly, leading to incorrect target URLs or lost state.
Prior to v2.1.0, `node-fetch-cookies` had issues with correctly processing certain redirect status codes.
fix
Upgrade `node-fetch-cookies` to version `2.1.0` or newer.
ERR_REQUIRE_ESM: Must use import to load ES Module: .../node_modules/node-fetch-cookies/index.js
Attempting to `require()` `node-fetch-cookies` in a CommonJS module when the package is primarily an ES Module.
fix
Convert your project or relevant file to use ES Modules (`type: "module"` in `package.json`, `.mjs` extension) and use `import { fetch, CookieJar } from 'node-fetch-cookies';`. Alternatively, use dynamic import: `const { fetch, CookieJar } = await import('node-fetch-cookies');`.
Upgrade
Version history
2.1.1latest on npm
Audit
Dependencies
node-fetchrequiredCore dependency providing the underlying fetch API functionality which this package wraps.
Agent activity
4 hits · last 30 days
node
4
Resources
node-fetch-cookies — npm install node-fetch-cookies · libregistry