Registry / http-networking / set-cookie-parser

set-cookie-parser

JSON →
library3.1.0jsnpmunverified

set-cookie-parser is a JavaScript/TypeScript library designed to parse `Set-Cookie` HTTP response headers into structured JavaScript objects. It currently stands at stable version 3.1.0 and is actively maintained, with a healthy release cadence and recent updates. The library is highly versatile, accepting various inputs including single header strings, arrays of header strings, Node.js `http.ServerResponse` objects, and `fetch()` `Response` objects. A key differentiator is its ability to return either an array of cookie objects or a map where cookie names are keys, depending on configuration. Each parsed cookie object provides a comprehensive set of attributes, including `name`, `value`, `path`, `domain`, `expires` (as `Date` objects), `maxAge`, `secure`, `httpOnly`, `sameSite`, and `partitioned`. It ships with built-in TypeScript types, ensuring robust type checking and improved developer experience in modern TypeScript projects.

npm install set-cookie-parser
INSTALL
IMPORT
SIG · SET-COOKIE-PARSER
S
set-cookie-parser
http-networkingjavascriptv3.1.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
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 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

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

parseSetCookie
import { parseSetCookie } from 'set-cookie-parser';
import setCookieParser from 'set-cookie-parser';
The primary parsing function is a named export. Default import is not provided.
parseSetCookie
const { parseSetCookie } = require('set-cookie-parser');
const setCookieParser = require('set-cookie-parser'); const cookies = setCookieParser.parseSetCookie(res);
When using CommonJS, `parseSetCookie` is a named export requiring destructuring. Direct property access on the `require` result is also possible but less idiomatic for this pattern.
Cookie
import type { Cookie } from 'set-cookie-parser';
The type definition for a parsed cookie object, useful for TypeScript projects. It aligns with the structure described in the README (name, value, path, etc.).

This quickstart demonstrates how to parse `Set-Cookie` headers from an HTTP response object, iterating and logging the properties of each parsed cookie.

import * as http from 'node:http'; import { parseSetCookie } from 'set-cookie-parser'; // Example: Fetching from a URL and parsing Set-Cookie headers http.get('http://example.com', function(res) { const cookies = parseSetCookie(res, { decodeValues: true // default: true }); console.log('Parsed Cookies from example.com:'); if (cookies.length === 0) { console.log('No Set-Cookie headers found.'); } else { cookies.forEach(cookie => { console.log(`- Name: ${cookie.name}, Value: ${cookie.value}`); if (cookie.expires) console.log(` Expires: ${cookie.expires.toISOString()}`); if (cookie.maxAge) console.log(` Max-Age: ${cookie.maxAge} seconds`); if (cookie.domain) console.log(` Domain: ${cookie.domain}`); if (cookie.path) console.log(` Path: ${cookie.path}`); if (cookie.secure) console.log(` Secure: true`); if (cookie.httpOnly) console.log(` HttpOnly: true`); if (cookie.sameSite) console.log(` SameSite: ${cookie.sameSite}`); if (cookie.partitioned) console.log(` Partitioned: true`); console.log('---'); }); } }).on('error', (e) => { console.error(`Error fetching: ${e.message}`); });
Debug
Known issues
gotchaWhen setting cookies via `express.js`'s `res.cookie()` method using `maxAge` values obtained from `set-cookie-parser`, remember that `express` expects `maxAge` in milliseconds, while `set-cookie-parser` provides it in seconds. Always multiply `maxAge` by 1000 before passing it to `res.cookie()` to avoid unexpectedly short-lived cookies.
fix
Ensure `maxAge` is converted: `res.cookie('name', 'value', { maxAge: cookie.maxAge * 1000 });`
affects: >=1.0.0
gotchaThe library copies `sameSite` values directly from the `Set-Cookie` header without performing any validation. If the `sameSite` attribute in the header contains an invalid or unexpected string, `set-cookie-parser` will return it verbatim in the parsed cookie object. Consumer applications should implement their own validation if strict adherence to `SameSite` values ('Strict', 'Lax', 'None') is required.
fix
Implement client-side validation for `cookie.sameSite` values, e.g., `if (!['Strict', 'Lax', 'None'].includes(cookie.sameSite)) { /* handle invalid value */ }`
affects: >=1.0.0
gotchaThe `parseSetCookie` function accepts various inputs including raw strings, arrays of strings, `http.ServerResponse` objects, or `fetch.Response` objects. Passing an `http.IncomingMessage` (request object) instead of a `http.ServerResponse` (response object) might lead to unexpected or empty results, as `Set-Cookie` headers are found on responses, not requests.
fix
Always ensure the input to `parseSetCookie` is an object from which `Set-Cookie` headers can be legitimately read, typically a response object.
affects: >=1.0.0
Errors
Common errors & fixes
ReferenceError: parseSetCookie is not defined
Attempting to use `parseSetCookie` without correctly importing it as a named export, or using a default import which does not exist.
fix
For ES Modules: `import { parseSetCookie } from 'set-cookie-parser';` For CommonJS: `const { parseSetCookie } = require('set-cookie-parser');`
TypeError: Cannot read properties of undefined (reading 'name') (or similar for cookie properties)
This typically occurs when trying to access properties like `name` or `value` on a parsed cookie object, but `parseSetCookie` returned an empty array or `undefined` because no `Set-Cookie` headers were present or the input was invalid.
fix
Always check if the `cookies` array (or map) is populated before attempting to access its elements or properties, and ensure the input to `parseSetCookie` is valid and contains `Set-Cookie` headers.
TypeError: input.headers.get is not a function
This error usually occurs when you pass a plain object or an incorrect type to `parseSetCookie` when it expects a `fetch.Response` object. The `fetch.Response` object has a `headers` property which is a `Headers` object, and this object has a `get` method.
fix
If you are working with `fetch` responses, ensure you pass the actual `Response` object or its `response.headers` object directly to `parseSetCookie`.
Upgrade
Version history
3.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
12 hits · last 30 days
node
8
OpenAI (training)
1
Resources
set-cookie-parser — npm install set-cookie-parser · libregistry