Modern Cookie Utility for Browsers
js-cookie-next is a native-first, TypeScript-first utility library designed for managing browser cookies with a focus on modern web standards. It transparently supports the Asynchronous Cookie Store API where available and falls back to `document.cookie` for broader compatibility. Currently at version 1.0.1, the library maintains a stable release cadence, with updates primarily driven by new browser features or bug fixes rather than frequent major API changes. Key differentiators include its zero-dependency footprint, small size (< 2 KB gzipped), SSR-safe imports that make sync APIs no-ops in non-browser environments, and built-in support for Partitioned Cookies (CHIPS) via a simple preset option. It prioritizes type safety and a clear API for both synchronous and asynchronous cookie operations, catering to contemporary web development practices.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'cookie')
cause Attempting to use synchronous cookie APIs (`get`, `set`, `remove`) in a Node.js or SSR environment where the `document` object is not available.fixThe library's sync APIs are designed to be no-ops in SSR. If you encounter this error in a non-browser environment, it indicates a misconfiguration or an attempt to use client-side logic on the server. Consider conditional rendering or ensure code runs only in the browser. -
Cookie 'mycookie' not found after removal.
cause The `remove()` function was called without matching the `path` and `domain` options that were used when the cookie was originally set.fixWhen removing a cookie, ensure that all relevant options (like `path` and `domain`) are identical to those used during the `set()` operation. For example, if set with `set('mycookie', 'value', { path: '/app' })`, remove with `remove('mycookie', { path: '/app' })`. -
Cookie 'mycookie' not appearing in browser despite `set()` call.
cause Often caused by incorrect `sameSite` and `secure` combinations, particularly `sameSite: "none"` without `secure: true`, or attempting to set a secure cookie over an insecure HTTP connection.fixIf `sameSite` is `"none"`, you *must* also set `secure: true`. Ensure your site is served over HTTPS when setting `secure` cookies. Also, check for `domain` mismatches if you're attempting cross-subdomain cookies.
Warnings
- gotcha When setting a cookie with `sameSite: "none"`, modern browsers require the `secure: true` option to be explicitly set. Failing to do so will result in the cookie being rejected.
- gotcha To successfully remove a cookie, the `path` and `domain` options used in the `remove` call must exactly match those specified when the cookie was originally set.
- gotcha The synchronous APIs (`get`, `set`, `remove`) are designed for browser environments. When imported in a Server-Side Rendering (SSR) context where `document` is undefined, these functions will act as no-ops to prevent runtime errors.
- gotcha The `mode: "partitioned"` option is a convenience preset that expands to `partitioned: true`, `secure: true`, and `sameSite: "none"`. Browser support for Partitioned Cookies (CHIPS) is still evolving, and the cookie may not be partitioned in unsupported browsers.
Install
-
npm install js-cookie-next -
yarn add js-cookie-next -
pnpm add js-cookie-next
Imports
- get
const { get } = require('js-cookie-next')import { get } from 'js-cookie-next' - set
import set from 'js-cookie-next'
import { set } from 'js-cookie-next' - getAsync
import { get, set } from 'js-cookie-next/async'import { getAsync } from 'js-cookie-next' - CookieOptions
import { CookieOptions } from 'js-cookie-next'import type { CookieOptions } from 'js-cookie-next'
Quickstart
import { get, set, remove, getAsync, setAsync, removeAsync } from "js-cookie-next";
// Basic synchronous usage
set("theme", "dark", { path: "/", sameSite: "lax" });
console.log('Current theme (sync):', get("theme"));
remove("theme", { path: "/" });
console.log('Theme after removal (sync):', get("theme"));
// Asynchronous usage with fallback
async function manageAsyncCookie() {
await setAsync("session_id", "user_xyz", { expires: 7, secure: true, sameSite: "none" });
const sessionId = await getAsync("session_id");
console.log('Session ID (async):', sessionId);
// Example of a partitioned cookie (CHIPS)
await setAsync("widget_data", "value123", { mode: "partitioned" });
console.log('Widget data set with CHIPS (async).');
await removeAsync("session_id");
console.log('Session ID after removal (async):', await getAsync("session_id"));
}
manageAsyncCookie();