Getting started
Install
sh
npm install browser-cookie-utils@2sh
pnpm add browser-cookie-utils@2sh
yarn add browser-cookie-utils@2Import
js
import { setCookie, getCookie, deleteCookie } from 'browser-cookie-utils';js
const { setCookie, getCookie, deleteCookie } = require('browser-cookie-utils');html
<script src="https://cdn.jsdelivr.net/npm/browser-cookie-utils@2/dist/browser-cookie-utils.umd.js"></script>
<script>
browserCookieUtils.setCookie('theme', 'dark');
</script>Pin the major version
Use @2 in CDN URLs so a future breaking release doesn't break your page.
First cookie
js
import { setCookie, getCookie, deleteCookie } from 'browser-cookie-utils';
setCookie('theme', 'dark'); // 1 hour, path=/, SameSite=Lax, Secure on HTTPS
getCookie('theme'); // 'dark'
deleteCookie('theme');Next: Setting cookies covers expiry, domains, paths and every other option.
Available builds
| File | Format | Notes |
|---|---|---|
browser-cookie-utils.js | ESM | Non-minified |
browser-cookie-utils.min.js | ESM | Minified (used by import) |
browser-cookie-utils.cjs | CJS | Non-minified |
browser-cookie-utils.min.cjs | CJS | Minified (used by require) |
browser-cookie-utils.umd.js | UMD | Minified; <script> tag, AMD or CommonJS |
browser-cookie-utils.d.ts | Types | TypeScript declarations (.d.cts for CJS) |
browser-cookie-utils.min.js.map | Source map | For the minified ESM build (npm only) |
Only the UMD build creates the window.browserCookieUtils global.
Environments
- Browsers: all modern evergreen browsers, no polyfills needed. Builds use ES2015+ syntax, so Internet Explorer isn't supported.
- SSR / Node: safe to import. Without
document, the functions do nothing and returnnull,false,{}or[].