{"id":47314,"library":"cookie-storage","title":"CookieStorage - Web Storage Interface for Cookies","description":"cookie-storage provides a Web Storage API-compatible interface for browser cookies, implementing the same methods as localStorage/sessionStorage (getItem, setItem, removeItem, clear, key, length) with additional cookie-specific options like path, domain, expires, secure, and sameSite. Current stable version is 6.1.0, released in 2023. The library is lightweight, ships TypeScript definitions, and supports ESM imports. Unlike raw document.cookie manipulation, it offers a cleaner API and automatic cookie string encoding/decoding. It is ideal for cross-browser cookie management with a familiar storage interface, but note that cookie storage is limited to ~4KB per cookie and 20-50 cookies per domain.","status":"active","version":"6.1.0","language":"javascript","source_language":"en","source_url":"https://github.com/bouzuya/cookie-storage","tags":["javascript","browser","cookie","storage","typescript"],"install":[{"cmd":"npm install cookie-storage","lang":"bash","label":"npm"},{"cmd":"yarn add cookie-storage","lang":"bash","label":"yarn"},{"cmd":"pnpm add cookie-storage","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Export is named, not default. ESM-only since v6; no CommonJS support.","wrong":"import CookieStorage from 'cookie-storage'","symbol":"CookieStorage","correct":"import { CookieStorage } from 'cookie-storage'"},{"note":"Options interface is a type; use type import for TypeScript.","wrong":"import { CookieStorageOptions } from 'cookie-storage'","symbol":"CookieStorageOptions","correct":"import type { CookieStorageOptions } from 'cookie-storage'"},{"note":"CommonJS require with destructuring for named export. Only works if your bundler supports ESM interop; official support is ESM.","wrong":"const CookieStorage = require('cookie-storage')","symbol":"CookieStorage","correct":"const { CookieStorage } = require('cookie-storage')"}],"quickstart":{"code":"import { CookieStorage } from 'cookie-storage';\n\nconst cookieStorage = new CookieStorage();\n\n// Set a cookie with options\ncookieStorage.setItem('username', 'john_doe', {\n  path: '/',\n  secure: true,\n  sameSite: 'Lax'\n});\n\n// Get a cookie\nconst username = cookieStorage.getItem('username');\nconsole.log(username); // 'john_doe'\n\n// List all cookies\nfor (let i = 0; i < cookieStorage.length; i++) {\n  console.log(`${cookieStorage.key(i)}: ${cookieStorage.getItem(cookieStorage.key(i))}`);\n}\n\n// Remove a cookie\ncookieStorage.removeItem('username');\n\n// Clear all cookies\ncookieStorage.clear();","lang":"typescript","description":"Demonstrates creating a CookieStorage instance, setting/getting cookies with options, iterating over storage, and removing/clearing cookies."},"warnings":[{"fix":"Update your import to ESM syntax: import { CookieStorage } from 'cookie-storage'","message":"Version 6.0.0 removed CommonJS (CJS) support. Only ESM imports are now valid.","severity":"breaking","affected_versions":">=6.0.0"},{"fix":"Switch to named import: import { CookieStorage } from 'cookie-storage'","message":"In versions 5.x and earlier, the exported class was called 'CookieStorage' but could also be imported as default. This was removed in v6.","severity":"deprecated","affected_versions":">=5.0.0 <6.0.0"},{"fix":"Use a storage event listener or consider using localStorage for cross-tab sync.","message":"CookieStorage is not synchronized across browser tabs. Changes made in one tab are not reflected in another until the page reloads.","severity":"gotcha","affected_versions":"*"},{"fix":"Do not rely on order; iterate via length and key() if needed, but prefer storing a list of keys manually.","message":"The 'key(index)' method returns cookie names in no guaranteed order; order may vary across browsers and over time.","severity":"gotcha","affected_versions":"*"},{"fix":"Keep cookie sizes small and monitor total count. Use chunking or alternative storage for large data.","message":"Cookie size limit (~4096 bytes per cookie) and per-domain count limit (~50 cookies) apply. If exceeded, setItem may fail silently.","severity":"gotcha","affected_versions":"*"}],"env_vars":null,"search_vec":"'-50':106 '20':105 '2023':53 '4kb':101 '6.1.0':50 'addit':34 'api':15,73 'api-compat':14 'automat':75 'browser':19,85,111 'cleaner':72 'clear':30 'compat':16 'cooki':6,8,20,36,76,86,96,103,107,112 'cookie-specif':35 'cookie-storag':7 'cookiestorag':1 'cross':84 'cross-brows':83 'current':46 'definit':60 'document.cookie':67 'domain':41,109 'encoding/decoding':78 'esm':63 'expir':42 'familiar':90 'getitem':27 'ideal':81 'implement':21 'import':64 'interfac':4,17,92 'javascript':110 'key':31 'length':32 'librari':55 'lightweight':57 'like':39 'limit':99 'localstorage/sessionstorage':26 'manag':87 'manipul':68 'method':24 'note':94 'offer':70 'option':38 'path':40 'per':102,108 'provid':10 'raw':66 'releas':51 'removeitem':29 'samesit':45 'secur':43 'setitem':28 'ship':58 'specif':37 'stabl':47 'storag':3,9,13,91,97,113 'string':77 'support':62 'typescript':59,114 'unlik':65 'version':48 'web':2,12","created_at":"2026-06-07T16:50:53.623217+00:00","updated_at":"2026-06-07T16:50:53.623217+00:00","problems":[{"fix":"Change to: import { CookieStorage } from 'cookie-storage'","cause":"Using default import instead of named import in ESM","error":"Uncaught TypeError: CookieStorage is not a constructor"},{"fix":"Run 'npm install cookie-storage' and use correct import: const { CookieStorage } = require('cookie-storage')","cause":"Package not installed or CommonJS require with wrong path","error":"Error: Cannot find module 'cookie-storage'"},{"fix":"Add 'new': const cookieStorage = new CookieStorage()","cause":"Forget to instantiate CookieStorage (using the class directly)","error":"TypeError: cookieStorage.getItem is not a function"},{"fix":"Ensure document.cookie is available. For Node, use a cookie parser package or mock the API.","cause":"Using in a non-browser environment (Node.js, SSR) without a cookie polyfill","error":"CookieStorage is not defined"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":null,"cli_version":null,"type":"library","homepage":"https://github.com/bouzuya/cookie-storage","github":"https://github.com/bouzuya/cookie-storage","docs":null,"changelog":null,"pypi":null,"npm":"cookie-storage","openapi_spec":null,"status_page":null,"smithery":null,"categories":["storage"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-07","next_check":"2026-09-05","install_tag":null}}