# cookie — card for AI agents

HTTP cookie parsing and serialization for Node.js servers.

Category: Utilities · npm package `cookie` · latest version 2.0.1 (checked 2026-11-01)
Install: `npm install cookie`
License: MIT (permissive)

In your own product (generator, app, hosted service): **yes**. Permissive license: ship it inside your own product, closed or open source, and keep its license and copyright notice.

## Agent Skills for it
- cookie-sync from browserbase/skills: `npx skills add browserbase/skills --skill cookie-sync`
- cookie-copy from daymade/claude-code-skills: `npx skills add daymade/claude-code-skills --skill cookie-copy`

## Numbers (AgentGid data)
- Gid Score: 62.2 (#854 of all tools, 98 of 408 in its category)
- Used in 18 public projects set up for AI coding agents (0.3%)
- Downloads last week: 347,234,031 (as of 2026-11-24), +45.8% over 4 weeks
- GitHub stars: 1,538 (as of 2026-11-24)
- Last push: 2026-11-05

## Often used with
dotenv 56.2%, Zod 68.5%, React 76.7%, Framer Motion 43.8%, TanStack Query 42.5%, jose 41.1%

## Links
- AgentGid page: https://agentgid.com/tools/cookie/
- Homepage: https://github.com/jshttp/cookie#readme
- Repository: https://github.com/jshttp/cookie


## cookie: read and set browser cookies on your server

cookie is a small free JavaScript library for the server side of a website. It reads the cookies a browser sends, such as a login session or a chosen theme, and writes new ones with the right safety settings. Frameworks use it inside; you call it yourself in a plain Node.js server or a small function.

### Where to use it
- Keep a user logged in: After sign-in, your server sends a session cookie that page scripts cannot read and that only travels over HTTPS.
- Read who is visiting: On each request your server turns the browser's cookie line into a simple list of names and values, like session and theme.
- Remember a visitor's choice: Save a name, language or dark mode choice in a cookie and greet the visitor with it on the next visit.
- Log a user out: Send the same cookie back empty with a lifetime of zero, so the browser drops the session at once. (idea, not from the official pages)
- Check cookies your server sends: In tests, or in a proxy in front of another service, turn a Set-Cookie line back into its parts and check each setting.
- Cookies in a tiny server: A small Node.js server or serverless function without Express can still read and set cookies without a full framework. (idea, not from the official pages)

### How to start
- Install the package in your server project (it needs Node.js 22 or newer).

```
$ npm install cookie
```

- Import it as a module.

```
import * as cookie from "cookie";
```

- Read the request's cookies with parseCookie and send new ones with stringifySetCookie.

```
const cookieObject = cookie.parseCookie("foo=bar; equation=E%3Dmc%5E2");
// { foo: 'bar', equation: 'E=mc^2' }
```


### License
Free for any use, including paid and closed products, under the MIT license. Keep the copyright and license notice if you ship its files.

### Good to know
- Version 2 needs Node.js 22 or newer and loads only as a modern module; older Node.js versions are not supported.
- Version 2 renamed the old functions; code and examples written for 1.x or 0.x need the new names.
- A cookie without an expiry date or a lifetime is dropped when the browser session ends.
- It only reads and writes the cookie text. It does not sign, encrypt or store sessions, so a value from the browser can be anything.
- Versions before 0.7.0 let a crafted cookie name, path or domain change other cookie settings; current versions check them.

Checked 2026-11-11 against: https://github.com/jshttp/cookie/blob/master/README.md, https://github.com/jshttp/cookie/blob/master/LICENSE, https://github.com/jshttp/cookie/blob/master/package.json, https://github.com/jshttp/cookie/releases/tag/v2.0.0, https://github.com/jshttp/cookie/releases/tag/v1.1.0, https://github.com/jshttp/cookie/releases/tag/v1.0.0, https://github.com/jshttp/cookie/security/advisories/GHSA-pxg6-pf52-xh8x, https://registry.npmjs.org/cookie, https://registry.npmjs.org/cookie/-/cookie-2.0.2.tgz

### Mistakes to avoid
- Don't: cookie.parse(header) and cookie.serialize(name, value, options) from 0.x/1.x examples → Do: cookie.parseCookie(header) and cookie.stringifySetCookie({ name, value, ...options }) (2.0.0 removed the old names (our run showed typeof cookie.parse is "undefined") and stringifySetCookie takes only an object.)
- Don't: npm install cookie in a project on Node.js 18 or 20 → Do: Run Node.js 22+, or pin cookie@1.1.1 on older Node.js (2.x is ESM only and states that older Node.js versions are not supported.)
- Don't: maxAge: 7 * 24 * 60 * 60 * 1000  // milliseconds, as in Express res.cookie → Do: maxAge: 60 * 60 * 24 * 7  // seconds (maxAge here is in seconds, so a millisecond value keeps the cookie 1000 times longer than meant.)
- Don't: Building the cookie name, path or domain from user input → Do: Fix names, paths and domains in your code; put user data only in the value (Old versions let crafted names change other settings; current versions throw a TypeError (our run - cookie name is invalid).)
- Don't: Storing the user id itself in the cookie and trusting it on the next request → Do: Store a random session id (or a signed or encrypted value, e.g. with jose or iron-session) and look it up on the server (The library only turns header text into values; anyone can edit the cookie text in their browser.)

### Questions
- Why does cookie.parse not exist anymore? Version 2.0.0 removed the old names. Use parseCookie to read and stringifySetCookie to write.
- Can I still use require("cookie")? Only on Node.js 22 or newer, which can load modern modules with require. Older Node.js versions are not supported by 2.x.
- What happens if a cookie value cannot be decoded? The default decoder catches the error and returns the original text instead of failing.
- Should I set both maxAge and expires? The README warns that not every client follows the rule that maxAge wins, so if you set both they should point to the same moment.
- Can I use it in a commercial product? Yes. It is MIT licensed.

---
AgentGid's catalog is curated: every entry was checked against the maker's official pages (registry, repository, license and pricing pages) on the date shown; a license value shows the quote it stands on wherever AgentGid has one. Usage numbers come from AgentGid's own scan of public repositories set up for AI coding agents. Claims by makers are reported, not tested by AgentGid.
