Cookies.js Client-Side Cookie Manipulation

1.2.3 · abandoned · verified Tue Apr 21

Cookies.js is a lightweight client-side JavaScript library for managing browser cookies, adhering to RFC6265 standards. The current stable version is 1.2.3, which was released approximately eight years ago (as of April 2026), indicating that the project is likely abandoned or in a deep maintenance phase with no active development. It distinguishes itself by having no external dependencies, offering cross-browser compatibility (IE6+, Chrome, Firefox 3+, Safari 4+, Opera 10+), and being released into the public domain. The library supports AMD and CommonJS module loaders and includes specific provisions for use in Node.js environments that lack a native `window` object, requiring a factory method for initialization. It transparently handles URI encoding of cookie keys and values using UTF-8 representation.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates setting, getting, and expiring cookies using Cookies.js within a Node.js environment by simulating a browser's `window` object with `jsdom`.

import { JSDOM } from 'jsdom';
import * as CookiesFactory from 'cookies-js'; // Assuming it's a factory function for Node

// Simulate a browser window for Node.js using jsdom
const { window } = new JSDOM('<!doctype html><html><body></body></html>', { url: 'http://localhost' });

// Initialize Cookies.js with the simulated window object
const Cookies = (CookiesFactory as any)(window);

console.log('--- Initializing Cookies.js ---');

// Set a persistent cookie with an expiry date far in the future
Cookies.set('user_session', 'session_token_123abc', {
  expires: Infinity, // Sets a persistent cookie (effectively never expires for a browser)
  path: '/',
  secure: true, // Only send over HTTPS (if applicable)
  domain: 'localhost' // Restrict to a specific domain
});
console.log('Cookie "user_session" set to a persistent value.');

// Retrieve a cookie
const sessionToken = Cookies.get('user_session');
console.log(`Retrieved 'user_session': ${sessionToken}`);

// Check if cookies are enabled in the environment
if (Cookies.enabled) {
  console.log('Cookies are enabled in this environment.');
} else {
  console.log('Cookies are disabled.');
}

// Set a temporary cookie that expires in 5 seconds
Cookies.set('last_activity', Date.now().toString(), { expires: 5 });
console.log('Cookie "last_activity" set to expire in 5 seconds.');

// Immediately expire the 'user_session' cookie
Cookies.expire('user_session');
console.log('Cookie "user_session" immediately expired.');

// Attempt to retrieve the expired cookie (should be undefined)
const expiredSessionToken = Cookies.get('user_session');
console.log(`Attempted to retrieve expired 'user_session': ${expiredSessionToken || 'undefined'}.`);

view raw JSON →