{"id":11151,"library":"js-cookie","title":"JavaScript Cookie API","description":"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.","status":"active","version":"3.0.5","language":"javascript","source_language":"en","source_url":"git://github.com/js-cookie/js-cookie","tags":["javascript","cookie","cookies","browser","amd","commonjs","client","js-cookie","browserify"],"install":[{"cmd":"npm install js-cookie","lang":"bash","label":"npm"},{"cmd":"yarn add js-cookie","lang":"bash","label":"yarn"},{"cmd":"pnpm add js-cookie","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The library uses a default export for its primary API object since v3. Named imports like `{ Cookies }` will not work.","wrong":"import { Cookies } from 'js-cookie';","symbol":"Cookies","correct":"import Cookies from 'js-cookie';"},{"note":"CommonJS require style for Node.js environments or projects using bundlers configured for CommonJS. Ensure your environment supports this.","wrong":"import Cookies from 'js-cookie'; // In CommonJS","symbol":"Cookies","correct":"const Cookies = require('js-cookie');"},{"note":"When included directly via a <script> tag in a browser, the `Cookies` object is exposed globally on `window`.","symbol":"Cookies","correct":"Cookies.set('name', 'value'); // Accessed globally"}],"quickstart":{"code":"import Cookies from 'js-cookie';\n\n// Set a cookie valid across the entire site for 7 days\nCookies.set('my_app_token', 'your-auth-token-123', { expires: 7 });\nconsole.log('Cookie \"my_app_token\" set successfully.');\n\n// Set a cookie with specific path and secure flag (often used for sensitive data)\nCookies.set('user_preference', 'dark-theme', { expires: 30, path: '/', secure: true });\nconsole.log('Cookie \"user_preference\" set for 30 days, secure, path /.');\n\n// Read a specific cookie\nconst token = Cookies.get('my_app_token');\nconsole.log('Retrieved token:', token);\n\n// Read all visible cookies\nconst allCookies = Cookies.get();\nconsole.log('All visible cookies:', allCookies);\n\n// Delete a cookie (must provide same path/domain if they were set)\nCookies.remove('my_app_token');\nconsole.log('Cookie \"my_app_token\" removed (site-wide).');\n\n// Example of deleting a cookie that was set with a specific path\nCookies.set('temp_data', 'some-value', { path: '/temp' });\n// Cookies.remove('temp_data'); // This would fail if path was not explicitly set during removal\nCookies.remove('temp_data', { path: '/temp' });\nconsole.log('Cookie \"temp_data\" (with path /temp) removed.');","lang":"javascript","description":"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."},"warnings":[{"fix":"Refactor code to use `Cookies.withAttributes()` to create API instances with predefined attributes. Example: `const api = Cookies.withAttributes({ path: '/', secure: true }); api.set('key', 'value');`","message":"The `defaults` object for setting global cookie attributes was removed in v3.0.0. Attempting to use `Cookies.defaults` will result in an error.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Ensure `Cookies.remove('name', { path: '/specific-path', domain: '.example.com' });` includes all relevant attributes that were specified during the cookie's creation.","message":"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.","severity":"gotcha","affected_versions":">=2.0.0"},{"fix":"Understand that cookie visibility for reading is determined by the browser based on the current URL, not by attributes passed to `get()`. Ensure cookies are set with appropriate `path` and `domain` for the desired visibility.","message":"`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.","severity":"gotcha","affected_versions":">=2.0.0"},{"fix":"Thoroughly test cookie interactions after upgrading, especially if your application relies on specific encoding behaviors from pre-v3 versions or interacts with backend systems with strict encoding expectations. No direct code fix is typically required, but awareness is crucial.","message":"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.","severity":"breaking","affected_versions":">=3.0.0-beta.4"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Use `Cookies.withAttributes({ expires: 7, path: '/' })` to create a new API instance with predefined attributes instead of setting global defaults.","cause":"Attempting to access or modify the `Cookies.defaults` object, which was removed in version 3.0.0.","error":"Uncaught TypeError: Cookies.defaults is undefined"},{"fix":"For 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.","cause":"Trying to use `require('js-cookie')` in a browser environment without a CommonJS-aware bundler (e.g., Webpack, Rollup, Browserify).","error":"ReferenceError: require is not defined"},{"fix":"Ensure 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.","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.","error":"Cookie 'myCookie' not removed (observed behavior, no console error)"},{"fix":"Use the default import syntax: `import Cookies from 'js-cookie';`","cause":"Attempting to import `Cookies` using a named import style (`import { Cookies } from 'js-cookie';`) when the library provides a default export.","error":"SyntaxError: Named export 'Cookies' not found. The requested module 'js-cookie' does not provide an export named 'Cookies'"}],"ecosystem":"npm"}