es-cookie

1.5.0 · active · verified Tue Apr 21

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

Warnings

Install

Imports

Quickstart

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.

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);

view raw JSON →