A simple, lightweight JavaScript API for handling cookies
npx @tessl/cli install tessl/npm-js-cookie@2.2.0JS 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.
npm install js-cookieESM (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// 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" });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);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);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);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();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);The following attributes can be passed to Cookies.set() or set in Cookies.defaults:
number | Datestring"/"stringbooleanfalseJS 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: [...] }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// 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
}