or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-js-cookie

A simple, lightweight JavaScript API for handling cookies

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/js-cookie@2.2.x

To install, run

npx @tessl/cli install tessl/npm-js-cookie@2.2.0

index.mddocs/

JS Cookie

JS Cookie is a simple, lightweight JavaScript API for handling browser cookies. It provides a clean interface for creating, reading, and deleting cookies with support for various attributes and encoding options. The library works in all browsers, has no dependencies, supports JSON data, and is RFC 6265 compliant.

Package Information

  • Package Name: js-cookie
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install js-cookie

Core Imports

ESM (recommended for modern bundlers):

import Cookies from "js-cookie";

For CommonJS:

const Cookies = require("js-cookie");

For AMD:

define(["js-cookie"], function(Cookies) {
  // Use Cookies
});

For browser (global):

// Available as window.Cookies
// Use Cookies.noConflict() to avoid namespace conflicts if needed
// Note: noConflict() only available in browser environment

Basic Usage

// Using ESM import or const Cookies = require("js-cookie") for CommonJS

// Set a simple cookie
Cookies.set("name", "value");

// Set a cookie with expiration (7 days from now)
Cookies.set("user", "john", { expires: 7 });

// Set a cookie with custom attributes
Cookies.set("token", "abc123", {
  expires: 1,
  path: "/admin",
  domain: ".example.com",
  secure: true
});

// Read a cookie
const name = Cookies.get("name"); // "value"
const missing = Cookies.get("missing"); // undefined

// Read all cookies
const allCookies = Cookies.get(); // { name: "value", user: "john", ... }

// Remove a cookie
Cookies.remove("name");

// Remove a cookie with specific attributes (must match set attributes)
Cookies.remove("token", { path: "/admin", domain: ".example.com" });

Capabilities

Cookie Creation

Creates or updates a cookie with optional attributes.

/**
 * Creates or updates a cookie
 * @param {string} key - Cookie name
 * @param {string|object|array} value - Cookie value (objects/arrays are JSON stringified)
 * @param {object} [attributes] - Cookie attributes
 * @returns {string} The cookie string that was set
 */
Cookies.set(key, value, attributes);

Cookie Reading

Reads cookie values, optionally parsing JSON data.

/**
 * Reads a cookie value as raw string
 * @param {string} [key] - Cookie name. If omitted, returns all cookies
 * @returns {string|undefined|object} Single value, undefined, or all cookies object
 */
Cookies.get(key);

/**
 * Reads a cookie value and parses it as JSON
 * @param {string} [key] - Cookie name. If omitted, returns all cookies parsed as JSON
 * @returns {any|undefined|object} Parsed JSON value, undefined, or all cookies parsed
 */
Cookies.getJSON(key);

Cookie Deletion

Removes cookies from the browser.

/**
 * Deletes a cookie by setting it to expire in the past
 * @param {string} key - Cookie name to delete
 * @param {object} [attributes] - Must match the path and domain used when setting
 * @returns {undefined}
 */
Cookies.remove(key, attributes);

Configuration

Global default attributes and namespace management.

/**
 * Default attributes applied to all cookie operations
 * @type {object}
 */
Cookies.defaults;

/**
 * Restores original window.Cookies and returns js-cookie API 
 * BROWSER ONLY: Not available in AMD/CommonJS environments
 * @returns {object} Cookies API object with all cookie methods
 */
Cookies.noConflict();

Custom Converters

Creates new Cookies instances with custom encoding/decoding.

/**
 * Creates a new Cookies instance with custom encoding/decoding
 * @param {function|object} converter - Function for read-only conversion, or object with read/write methods
 * @returns {object} New Cookies API instance with custom converter applied
 */
Cookies.withConverter(converter);

Cookie Attributes

The following attributes can be passed to Cookies.set() or set in Cookies.defaults:

expires

  • Type: number | Date
  • Description: Cookie expiration (Number = days from now, Date = specific date)
  • Default: Session cookie (expires when browser closes)

path

  • Type: string
  • Description: Cookie path scope
  • Default: "/"

domain

  • Type: string
  • Description: Cookie domain scope
  • Default: Current domain

secure

  • Type: boolean
  • Description: Cookie only sent over HTTPS
  • Default: false

JSON Support

JS Cookie provides automatic JSON handling for objects and arrays:

// Setting objects/arrays - automatically JSON.stringify'd
Cookies.set("user", { name: "John", age: 30 });
Cookies.set("items", ["apple", "banana", "cherry"]);

// Reading as raw string
Cookies.get("user"); // '{"name":"John","age":30}'

// Reading as parsed JSON
Cookies.getJSON("user"); // { name: "John", age: 30 }
Cookies.getJSON("items"); // ["apple", "banana", "cherry"]

// Reading all cookies as JSON
Cookies.getJSON(); // { user: { name: "John", age: 30 }, items: [...] }

Custom Encoding/Decoding

Create custom Cookies instances for special encoding needs:

// Custom read converter
const customCookies = Cookies.withConverter(function (value, name) {
  if (name === "special") {
    return unescape(value);
  }
  // Default decoding for other cookies
});

// Custom read/write converters
const encodedCookies = Cookies.withConverter({
  read: function (value, name) {
    // Custom read logic
    return decodeSpecialFormat(value);
  },
  write: function (value, name) {
    // Custom write logic
    return encodeSpecialFormat(value);
  }
});

customCookies.get("special"); // Uses custom decoder
encodedCookies.set("data", "value"); // Uses custom encoder

Advanced Examples

// Set default attributes for all cookies
Cookies.defaults = {
  expires: 7,
  path: "/",
  secure: true
};

// All subsequent cookies will use these defaults
Cookies.set("session", "abc123"); // Will expire in 7 days, be secure

// Override defaults for specific cookies
Cookies.set("temp", "data", { expires: 1 }); // Expires in 1 day instead of 7

// Namespace conflict resolution (browser only)
const myCookies = Cookies.noConflict();
// Original window.Cookies is restored
// Use myCookies for js-cookie functionality

// Complex cookie with all attributes
Cookies.set("complex", { user: "admin", role: "superuser" }, {
  expires: new Date("2024-12-31"),
  path: "/admin",
  domain: ".mysite.com",
  secure: true
});

// Read and validate
const complexData = Cookies.getJSON("complex");
if (complexData && complexData.role === "superuser") {
  // Handle admin user
}