JavaScript Cookie API

3.0.5 · active · verified Sun Apr 19

js-cookie is a simple, lightweight JavaScript library designed for handling browser cookies. Currently stable at version 3.0.5, it provides a minimal and dependency-free API for setting, getting, and deleting cookies. It supports ES Modules, CommonJS, and AMD, and is compatible with all major browsers. The library is RFC 6265 compliant and known for its small footprint (under 800 bytes gzipped). Releases appear to be somewhat infrequent but consistent, with bug fix releases for the 3.x series. Its key differentiator is its small size and lack of dependencies, focusing solely on efficient client-side cookie management without extra features, making it ideal for browser environments.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to set, get, and remove cookies using js-cookie, including options for expiration, path, and secure flag, and highlights important considerations for cookie deletion.

import Cookies from 'js-cookie';

// Set a cookie valid across the entire site for 7 days
Cookies.set('my_app_token', 'your-auth-token-123', { expires: 7 });
console.log('Cookie "my_app_token" set successfully.');

// Set a cookie with specific path and secure flag (often used for sensitive data)
Cookies.set('user_preference', 'dark-theme', { expires: 30, path: '/', secure: true });
console.log('Cookie "user_preference" set for 30 days, secure, path /.');

// Read a specific cookie
const token = Cookies.get('my_app_token');
console.log('Retrieved token:', token);

// Read all visible cookies
const allCookies = Cookies.get();
console.log('All visible cookies:', allCookies);

// Delete a cookie (must provide same path/domain if they were set)
Cookies.remove('my_app_token');
console.log('Cookie "my_app_token" removed (site-wide).');

// Example of deleting a cookie that was set with a specific path
Cookies.set('temp_data', 'some-value', { path: '/temp' });
// Cookies.remove('temp_data'); // This would fail if path was not explicitly set during removal
Cookies.remove('temp_data', { path: '/temp' });
console.log('Cookie "temp_data" (with path /temp) removed.');

view raw JSON →