JavaScript Cookie API
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
-
Uncaught TypeError: Cookies.defaults is undefined
cause Attempting to access or modify the `Cookies.defaults` object, which was removed in version 3.0.0.fixUse `Cookies.withAttributes({ expires: 7, path: '/' })` to create a new API instance with predefined attributes instead of setting global defaults. -
ReferenceError: require is not defined
cause Trying to use `require('js-cookie')` in a browser environment without a CommonJS-aware bundler (e.g., Webpack, Rollup, Browserify).fixFor browser-only usage, either include the library via a `<script>` tag and access `window.Cookies` globally, or use an ES module `import Cookies from 'js-cookie';` with a compatible bundler setup. -
Cookie 'myCookie' not removed (observed behavior, no console error)
cause `Cookies.remove()` was called without specifying the `path` or `domain` attributes that were used when the cookie was originally set, leading to the cookie not being found and deleted by the browser.fixEnsure that `Cookies.remove('myCookie', { path: '/specific-path', domain: '.example.com' });` includes all relevant attributes (`path`, `domain`) that were used when the cookie was initially created. -
SyntaxError: Named export 'Cookies' not found. The requested module 'js-cookie' does not provide an export named 'Cookies'
cause Attempting to import `Cookies` using a named import style (`import { Cookies } from 'js-cookie';`) when the library provides a default export.fixUse the default import syntax: `import Cookies from 'js-cookie';`
Warnings
- breaking The `defaults` object for setting global cookie attributes was removed in v3.0.0. Attempting to use `Cookies.defaults` will result in an error.
- gotcha When deleting a cookie, `Cookies.remove()` requires the same `path` and `domain` attributes that were used when the cookie was originally set. Failing to provide these attributes will result in the cookie not being removed.
- gotcha `Cookies.get()` does not accept or utilize attributes like `domain` or `path` for reading. It only retrieves cookies that are visible from the current document context based on the browser's native cookie rules.
- breaking In v3.0.0-beta.4 (and subsequent v3 releases), the encoding/decoding implementation was revised. It now only encodes characters strictly necessary (`;` and `=` in the cookie name, and `;` in the cookie value) aligning with user agent rules (RFC 6265 section 5.2). This change might affect compatibility with cookies set by older versions or by servers expecting stricter encoding.
Install
-
npm install js-cookie -
yarn add js-cookie -
pnpm add js-cookie
Imports
- Cookies
import { Cookies } from 'js-cookie';import Cookies from 'js-cookie';
- Cookies
import Cookies from 'js-cookie'; // In CommonJS
const Cookies = require('js-cookie'); - Cookies
Cookies.set('name', 'value'); // Accessed globally
Quickstart
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.');