Lightweight, dependency-free JavaScript helpers for getting, setting, and deleting browser cookies with modern browser support.
npm install browser-cookie-utils[![License: MIT][license-image]][license-url]
[![NPM version][npm-image]][npm-url]
[![Build][build-image]][build-url]
[![Downloads][jsdelivr-image]][jsdelivr-url]
Lightweight, dependency-free JavaScript helpers for getting, setting, and deleting browser cookies safely and consistently in modern browsers.
- Zero runtime dependencies
- Browser-only (no Node.js required in production)
- Simple, ergonomic API
- Cookie expiration with time units
- Multi-domain cookie support
- Automatic Secure flag on HTTPS
- SameSite support (Lax, Strict, None)
- Path customization
- Safe encoding / decoding
``sh`
npm install browser-cookie-utils@2
#### ESM import (modern bundlers):
`js`
import { setCookie, getCookie, deleteCookie } from 'browser-cookie-utils';
#### CJS import (Node.js / legacy bundlers):
`js`
const { getCookie, setCookie, deleteCookie } = require('browser-cookie-utils');
`html`
| File | Format | Notes |
| --------------------------------- | ---------- | --------------------------------------- |
| browser-cookie-utils.js | ESM | Non-minified, modern bundlers |browser-cookie-utils.min.js
| | ESM | Minified, production-ready (ES module) |browser-cookie-utils.cjs.js
| | CJS | Non-minified, Node.js / CommonJS |browser-cookie-utils.cjs.min.js
| | CJS | Minified, production-ready |browser-cookie-utils.umd.js
| | UMD | Legacy browsers / script tag & CommonJS |browser-cookie-utils.min.js.map
| | Source map | For minified ESM |
> Note: The library attaches itself to window.browserCookieUtils in the browser.
`js
import { getCookie, setCookie, deleteCookie } from 'browser-cookie-utils';
setCookie('theme', 'dark');
`
`js
const { getCookie, setCookie, deleteCookie } = require('browser-cookie-utils');
setCookie('theme', 'dark');
`
`js`
> The global browserCookieUtils is available only when using the UMD build.
`js
import { setCookie } from 'browser-cookie-utils';
setCookie('theme', 'dark');
`
Creates a cookie with:
- 1 hour expiration
- path=/SameSite=Lax
- Secure
- (automatically on HTTPS)
---
`js
import { setCookie } from 'browser-cookie-utils';
setCookie('session', 'abc123', {
timeToLive: 2,
unit: 'hour'
});
`
Supported units:
- hourday
- month
- (30 days)
---
`js
import { setCookie } from 'browser-cookie-utils';
setCookie('user', 'sami', {
timeToLive: 7,
unit: 'day',
domains: ['example.com', '.example.org'],
path: '/',
sameSite: 'Lax',
secure: true
});
`
#### Available options
| Option | Type | Default | Description |
| ------------ | ---------- | ---------------- | ------------------------ |
| timeToLive | number | 1 | Expiration duration |unit
| | string | hour | hour, day, month |domains
| | string[] | current hostname | Domains to set cookie on |path
| | string | / | Cookie path |sameSite
| | string | Lax | Lax, Strict, None |secure
| | boolean | auto | Forced Secure flag |
`js
import { setCookie } from 'browser-cookie-utils';
setCookie('crossSite', 'true', {
sameSite: 'None',
secure: true
});
`
> ⚠️ Browsers require Secure when using SameSite=None.
`js
import { getCookie } from 'browser-cookie-utils';
const theme = getCookie('theme');
`
Returns:
- string if foundnull
- if not found
`js
import { deleteCookie } from 'browser-cookie-utils';
deleteCookie('theme');
`
`js
import { deleteCookie } from 'browser-cookie-utils';
deleteCookie('user', {
domains: ['example.com'],
path: '/',
sameSite: 'Lax',
secure: true
});
`
> ⚠️ For deletion to succeed, domain, path, secure, and sameSite must match the original cookie.
> Browser (UMD) usage:
> When using the UMD build via a