{"id":15101,"library":"es-cookie","title":"es-cookie","description":"es-cookie is a lightweight, dependency-free JavaScript module designed for managing browser cookies, adhering to RFC 6265 specifications. It provides a simple, type-safe API for setting, getting, removing, and parsing cookies. The library is written in TypeScript and ships with native ES module definitions, making it well-suited for modern web development. The current stable version is 1.5.0, with recent updates including support for experimental `partitioned` cookies and fixes for cookie expiration handling. While originally inspired by `js-cookie`, es-cookie differentiates itself through its TypeScript rewrite, lean API, and explicit focus on ES module distribution, ensuring a modern, tree-shakable approach to cookie management without relying on external packages.","status":"active","version":"1.5.0","language":"javascript","source_language":"en","source_url":"git://github.com/theodorejb/es-cookie","tags":["javascript","cookies","typescript","module"],"install":[{"cmd":"npm install es-cookie","lang":"bash","label":"npm"},{"cmd":"yarn add es-cookie","lang":"bash","label":"yarn"},{"cmd":"pnpm add es-cookie","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The package is a native ES module. While `import * as Cookies` imports all named exports into a namespace object, attempting a default import (`import Cookies from 'es-cookie'`) will likely result in an undefined value or an error. CommonJS `require()` is not officially supported and should be avoided in favor of ES imports.","wrong":"import Cookies from 'es-cookie';\nconst Cookies = require('es-cookie');","symbol":"Cookies","correct":"import * as Cookies from 'es-cookie';"},{"note":"Named imports are recommended for tree-shaking and to only include the functions necessary for your application, leading to smaller bundle sizes.","wrong":"import { set as setCookie } from 'es-cookie/set';","symbol":"set, get, remove","correct":"import { set, get, remove } from 'es-cookie';"},{"note":"For TypeScript users, `CookieAttributes` defines the structure for the options object used in the `set` and `remove` functions, providing type safety for cookie attributes like `expires`, `path`, and `domain`.","symbol":"CookieAttributes","correct":"import type { CookieAttributes } from 'es-cookie';"}],"quickstart":{"code":"import * as Cookies from 'es-cookie';\n\n// Set a cookie that expires in 7 days, valid across the entire site\nCookies.set('my-session-token', 'your-secure-token-value-here', { expires: 7, secure: true, sameSite: 'Lax' });\n\n// Get a specific cookie by name\nconst token = Cookies.get('my-session-token');\nif (token) {\n  console.log('Retrieved token:', token);\n} else {\n  console.log('No token found.');\n}\n\n// Get all visible cookies\nconst allCookies = Cookies.getAll();\nconsole.log('All cookies:', allCookies);\n\n// Remove the cookie (important: use the same attributes if non-default)\nCookies.remove('my-session-token', { path: '/', secure: true, sameSite: 'Lax' });\nconsole.log('Token removed.');\n\n// Example of parsing a raw cookie string\nconst rawCookieString = 'some_cookie=some_value; another_cookie=another_value';\nconst parsed = Cookies.parse(rawCookieString);\nconsole.log('Parsed raw string:', parsed);","lang":"typescript","description":"Demonstrates setting a secure, expiring cookie, retrieving individual and all cookies, and correctly removing a cookie by matching attributes, along with parsing a raw cookie string."},"warnings":[{"fix":"Always specify `path` and `domain` options in `Cookies.remove()` if they were provided during `Cookies.set()`. For example, `Cookies.set('name', 'value', { path: '/app' }); Cookies.remove('name', { path: '/app' });`","message":"When removing a cookie, it is critical to provide the exact same `path` and `domain` attributes that were used when the cookie was initially set. Failing to match these attributes will prevent the cookie from being successfully deleted.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Ensure your project is configured for ES module imports (e.g., `type: \"module\"` in `package.json` for Node.js, or using a modern bundler like Webpack/Rollup/Vite). Use `import` statements exclusively for `es-cookie`.","message":"es-cookie is published as a native ES module. Direct `require()` statements in CommonJS environments are not officially supported and may lead to module resolution errors or unexpected behavior, especially in Node.js contexts or older bundlers.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Upgrade to `es-cookie@1.5.0` or newer to ensure that cookies with very long `expires` values are handled correctly and expire on the maximum possible date.","message":"Setting `expires` to a number larger than the maximum valid date (e.g., `Date.MAX_VALUE`) previously resulted in cookies not expiring correctly. This issue was fixed in v1.5.0.","severity":"gotcha","affected_versions":"<1.5.0"},{"fix":"When using the `partitioned` attribute, ensure target browsers support CHIPS. Be aware that this is an experimental standard and may evolve.","message":"Support for `partitioned` cookies was added in v1.5.0. This is an experimental feature designed for CHIPS (Cookies Having Independent Partitioned State). While supported, its broad browser compatibility and long-term specification stability should be monitored.","severity":"gotcha","affected_versions":">=1.5.0"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"Change `const Cookies = require('es-cookie');` to `import * as Cookies from 'es-cookie';` and ensure your project is configured for ES modules.","cause":"Attempting to use `require()` in an ES module environment or when `es-cookie` is exclusively an ES module.","error":"ReferenceError: require is not defined"},{"fix":"Verify and provide the identical `path` and `domain` attributes to `Cookies.remove()` as were used with `Cookies.set()`. For example: `Cookies.remove('myCookie', { path: '/some/path', domain: 'example.com' });`","cause":"The `path` or `domain` attributes provided to `Cookies.remove()` do not exactly match those used when the cookie was originally set.","error":"Cookie 'myCookie' was not deleted"},{"fix":"If you intend to use `Cookies.set`, `Cookies.get`, etc., change your import to `import * as Cookies from 'es-cookie';`. Alternatively, use named imports: `import { set, get } from 'es-cookie';`.","cause":"Incorrect import of the `es-cookie` module, specifically trying to import `Cookies` as a default export (`import Cookies from 'es-cookie'`) when it's a namespace import (`import * as Cookies from 'es-cookie'`) or attempting to access `set` on a non-existent or incorrectly imported `Cookies` object.","error":"TypeError: Cannot read properties of undefined (reading 'set') OR Cookies.set is not a function"}],"ecosystem":"npm"}