{"id":13621,"library":"node-fetch-cookies","title":"node-fetch-cookies","description":"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.","status":"active","version":"2.1.1","language":"javascript","source_language":"en","source_url":"https://github.com/jkhsjdhjs/node-fetch-cookies","tags":["javascript","cookie","cookie-jar","node-fetch","fetch"],"install":[{"cmd":"npm install node-fetch-cookies","lang":"bash","label":"npm"},{"cmd":"yarn add node-fetch-cookies","lang":"bash","label":"yarn"},{"cmd":"pnpm add node-fetch-cookies","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Core dependency providing the underlying fetch API functionality which this package wraps.","package":"node-fetch","optional":false}],"imports":[{"note":"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.","wrong":"import fetch from 'node-fetch-cookies';","symbol":"fetch","correct":"import { fetch } from 'node-fetch-cookies';"},{"note":"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.","wrong":"const CookieJar = require('node-fetch-cookies').CookieJar;","symbol":"CookieJar","correct":"import { CookieJar } from 'node-fetch-cookies';"},{"note":"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.","symbol":"nodeFetch","correct":"import { nodeFetch } from 'node-fetch-cookies';"},{"note":"This re-exports the `Headers` class from `node-fetch`, allowing consistent usage of `node-fetch`'s Headers API within a `node-fetch-cookies` context.","symbol":"Headers","correct":"import { Headers } from 'node-fetch-cookies';"}],"quickstart":{"code":"import { fetch, CookieJar } from \"node-fetch-cookies\";\nimport { promises as fs } from 'fs';\n\n(async () => {\n    const jarFilePath = \"jar.json\";\n\n    // Create a CookieJar instance\n    const cookieJar = new CookieJar(jarFilePath);\n\n    // Attempt to load cookies from file if it exists\n    try {\n        await cookieJar.load();\n        console.log(`Loaded cookies from ${jarFilePath}`);\n    } catch (error) {\n        if (error.code === 'ENOENT') {\n            console.log(`No existing cookie jar found at ${jarFilePath}. Starting fresh.`);\n        } else {\n            console.error(`Error loading cookie jar:`, error);\n            return;\n        }\n    }\n\n    // Make a request, cookies will be sent/received via the cookieJar\n    console.log(\"Making initial request to example.com...\");\n    let response = await fetch(cookieJar, \"https://example.com\");\n    console.log(`Status: ${response.status}`);\n    console.log(\"Headers received:\", response.headers.raw());\n\n    // You can now make another request to a different page on the same domain or a subdomain\n    // and the cookies from the first request will be automatically sent.\n    console.log(\"Making second request to example.com/path...\");\n    response = await fetch(cookieJar, \"https://example.com/path\");\n    console.log(`Status: ${response.status}`);\n\n    // Save the received cookies to disk\n    await cookieJar.save();\n    console.log(`Saved cookies to ${jarFilePath}`);\n})();","lang":"javascript","description":"Demonstrates initializing a CookieJar with file persistence, loading existing cookies, making multiple requests with automatic cookie handling, and saving updated cookies to disk."},"warnings":[{"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.","message":"Older versions (prior to v2.1.1) had a `TypeError` when deleting the `content-length` header during redirects that changed methods to `GET`.","severity":"breaking","affected_versions":"<2.1.1"},{"fix":"Upgrade to version `2.1.1` or newer to ensure correct cookie header handling during redirects.","message":"Prior to v2.1.1, duplicate cookie headers could be sent when following redirects, potentially leading to unexpected server behavior or rejection.","severity":"gotcha","affected_versions":"<2.1.1"},{"fix":"Upgrade to version `2.1.0` or newer for corrected and more robust HTTP redirect handling.","message":"Versions 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.","severity":"breaking","affected_versions":"<2.1.0"},{"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')`).","message":"The 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.","severity":"gotcha","affected_versions":">=2.0.0"}],"env_vars":null,"search_vec":"'14.13.0':102 '2.1.1':78 'across':46 'activ':82 'ad':133 'add':22 'address':84 'allow':41 'api':131 'around':12 'attribut':94 'base':61 'bug':86 'compon':127 'cooki':4,8,27,39,56,93,117,135,138,140 'cookie-jar':38,139 'core':123 'design':20 'develop':34 'differenti':114 'disk':67 'easili':36 'enabl':33 'environ':31,101 'es':105 'essenti':134 'explicit':73 'export':122 'familiar':130 'fetch':3,7,17,126,144,145 'file':60,65 'file-bas':59 'fix':87 'function':136 'handl':37,91 'http':26 'i/o':68 'in-memori':53 'includ':115 'instruct':74 'jar':40,141 'javascript':137 'json':64 'key':113 'leverag':104 'librari':18,50 'maintain':83 'manag':28,45 'memori':55 'minim':66 'modern':99 'modul':106 'multipl':47 'node':2,6,16,125,143 'node-fetch':15,124,142 'node-fetch-cooki':1,5 'node.js':30,100 'pars':95 'pattern':111 'persist':43,62,118 'popular':14 'primari':109 'primarili':97 'project':80 'provid':128 're':121 're-export':120 'recent':85 'redirect':90 'relat':88 'request':48 'robust':23 'save':71 'session':44 'simplifi':116 'specif':19 'storag':57 'support':24,51 'target':98 'usag':110 'use':63 'version':77 'wrapper':11","created_at":"2026-04-20T01:55:27.229006+00:00","updated_at":"2026-04-20T01:55:27.229006+00:00","problems":[{"fix":"Upgrade `node-fetch-cookies` to version `2.1.1` or newer.","cause":"A bug in older versions of `node-fetch-cookies` incorrectly tried to modify headers on redirected GET requests.","error":"TypeError: Attempted to delete 'content-length' header when following redirect and changing method to GET."},{"fix":"Update `node-fetch-cookies` to version `2.1.1` or higher.","cause":"A bug in older versions of `node-fetch-cookies` caused redundant cookie headers to be included after redirects.","error":"Duplicate 'cookie' headers are sent on subsequent requests after a redirect."},{"fix":"Upgrade `node-fetch-cookies` to version `2.1.0` or newer.","cause":"Prior to v2.1.0, `node-fetch-cookies` had issues with correctly processing certain redirect status codes.","error":"HTTP 301/302/303 redirects are not handled correctly, leading to incorrect target URLs or lost state."},{"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');`.","cause":"Attempting to `require()` `node-fetch-cookies` in a CommonJS module when the package is primarily an ES Module.","error":"ERR_REQUIRE_ESM: Must use import to load ES Module: .../node_modules/node-fetch-cookies/index.js"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/jkhsjdhjs/node-fetch-cookies","docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/node-fetch-cookies","openapi_spec":null,"status_page":null,"smithery":null,"categories":["http-networking","serialization"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-17","next_check":"2026-07-18","install_tag":null}}