es-cookie
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.
Common errors
-
ReferenceError: require is not defined
cause Attempting to use `require()` in an ES module environment or when `es-cookie` is exclusively an ES module.fixChange `const Cookies = require('es-cookie');` to `import * as Cookies from 'es-cookie';` and ensure your project is configured for ES modules. -
Cookie 'myCookie' was not deleted
cause The `path` or `domain` attributes provided to `Cookies.remove()` do not exactly match those used when the cookie was originally set.fixVerify 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' });` -
TypeError: Cannot read properties of undefined (reading 'set') OR Cookies.set is not a function
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.fixIf 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';`.
Warnings
- gotcha 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.
- gotcha 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.
- gotcha 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.
- gotcha 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.
Install
-
npm install es-cookie -
yarn add es-cookie -
pnpm add es-cookie
Imports
- Cookies
import Cookies from 'es-cookie'; const Cookies = require('es-cookie');import * as Cookies from 'es-cookie';
- set, get, remove
import { set as setCookie } from 'es-cookie/set';import { set, get, remove } from 'es-cookie'; - CookieAttributes
import type { CookieAttributes } from 'es-cookie';
Quickstart
import * as Cookies from 'es-cookie';
// Set a cookie that expires in 7 days, valid across the entire site
Cookies.set('my-session-token', 'your-secure-token-value-here', { expires: 7, secure: true, sameSite: 'Lax' });
// Get a specific cookie by name
const token = Cookies.get('my-session-token');
if (token) {
console.log('Retrieved token:', token);
} else {
console.log('No token found.');
}
// Get all visible cookies
const allCookies = Cookies.getAll();
console.log('All cookies:', allCookies);
// Remove the cookie (important: use the same attributes if non-default)
Cookies.remove('my-session-token', { path: '/', secure: true, sameSite: 'Lax' });
console.log('Token removed.');
// Example of parsing a raw cookie string
const rawCookieString = 'some_cookie=some_value; another_cookie=another_value';
const parsed = Cookies.parse(rawCookieString);
console.log('Parsed raw string:', parsed);