Registry / web-framework / cookies-js

cookies-js

JSON →
library1.2.3jsnpmunverified

Cookies.js is a lightweight client-side JavaScript library for managing browser cookies, adhering to RFC6265 standards. The current stable version is 1.2.3, which was released approximately eight years ago (as of April 2026), indicating that the project is likely abandoned or in a deep maintenance phase with no active development. It distinguishes itself by having no external dependencies, offering cross-browser compatibility (IE6+, Chrome, Firefox 3+, Safari 4+, Opera 10+), and being released into the public domain. The library supports AMD and CommonJS module loaders and includes specific provisions for use in Node.js environments that lack a native `window` object, requiring a factory method for initialization. It transparently handles URI encoding of cookie keys and values using UTF-8 representation.

npm install cookies-js
INSTALL
IMPORT
SIG · COOKIES-JS
C
cookies-js
web-frameworkjavascriptv1.2.3
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Cookies
✓ import * as Cookies from 'cookies-js';
✗ import Cookies from 'cookies-js';
For modern bundlers, this library is often consumed as a namespace import due to its CommonJS origins. A default import (`import Cookies from 'cookies-js'`) might not work or could result in `undefined`.
Cookies (CommonJS)
✓ const Cookies = require('cookies-js');
✗ import Cookies from 'cookies-js';
This is the standard CommonJS import for browser-like environments where a `window` object is globally available.
Cookies (Node.js/JSDOM factory)
✓ const { JSDOM } = require('jsdom'); const window = new JSDOM().window; const Cookies = require('cookies-js')(window);
✗ const Cookies = require('cookies-js');
In Node.js or other environments without a native `window` object, `cookies-js` exports a factory method that requires a `window` instance (e.g., from `jsdom`) to be explicitly passed.

Demonstrates setting, getting, and expiring cookies using Cookies.js within a Node.js environment by simulating a browser's `window` object with `jsdom`.

import { JSDOM } from 'jsdom'; import * as CookiesFactory from 'cookies-js'; // Assuming it's a factory function for Node // Simulate a browser window for Node.js using jsdom const { window } = new JSDOM('<!doctype html><html><body></body></html>', { url: 'http://localhost' }); // Initialize Cookies.js with the simulated window object const Cookies = (CookiesFactory as any)(window); console.log('--- Initializing Cookies.js ---'); // Set a persistent cookie with an expiry date far in the future Cookies.set('user_session', 'session_token_123abc', { expires: Infinity, // Sets a persistent cookie (effectively never expires for a browser) path: '/', secure: true, // Only send over HTTPS (if applicable) domain: 'localhost' // Restrict to a specific domain }); console.log('Cookie "user_session" set to a persistent value.'); // Retrieve a cookie const sessionToken = Cookies.get('user_session'); console.log(`Retrieved 'user_session': ${sessionToken}`); // Check if cookies are enabled in the environment if (Cookies.enabled) { console.log('Cookies are enabled in this environment.'); } else { console.log('Cookies are disabled.'); } // Set a temporary cookie that expires in 5 seconds Cookies.set('last_activity', Date.now().toString(), { expires: 5 }); console.log('Cookie "last_activity" set to expire in 5 seconds.'); // Immediately expire the 'user_session' cookie Cookies.expire('user_session'); console.log('Cookie "user_session" immediately expired.'); // Attempt to retrieve the expired cookie (should be undefined) const expiredSessionToken = Cookies.get('user_session'); console.log(`Attempted to retrieve expired 'user_session': ${expiredSessionToken || 'undefined'}.`);
Debug
Known issues
gotchaThe `cookies-js` package appears to be abandoned, with the last update (v1.2.3) occurring approximately eight years ago. While it remains functional for its core purpose, new feature development, bug fixes, or security patches are unlikely.
fix
For new projects, consider more actively maintained cookie management libraries. For existing projects, be aware of the lack of ongoing support and potential future compatibility issues or unpatched vulnerabilities.
affects: >=1.2.3
breakingVersion 1.0.0 marked a significant change in the project's licensing, moving it into the public domain. While no API-breaking changes occurred between v0.4.0 and v1.0.0, this represented a major shift in project governance.
fix
Review the licensing terms if your project's legal compliance depends on the pre-1.0.0 licensing model. The API itself remained stable from 0.4.0, so code changes are generally not required.
affects: <1.0.0
gotchaWhen using `cookies-js` in environments without a native `window` object (e.g., Node.js), the library exports a factory method that requires an explicit `window` instance to be passed during initialization.
fix
For Node.js, use a library like `jsdom` to create a `window` instance and pass it to the `cookies-js` factory: `const { JSDOM } = require('jsdom'); const window = new JSDOM().window; const Cookies = require('cookies-js')(window);`
affects: >=1.1.0
gotcha`cookies-js` strictly adheres to RFC6265 and expects all cookie keys and values to be URI encoded in UTF-8 representation. Incorrect encoding, especially when interoperating with server-side frameworks that use different encoding schemes, can lead to malformed cookies or data loss.
fix
Ensure all cookie keys and values are consistently URI encoded using UTF-8. For .NET users, specifically avoid `HttpUtility.UrlEncode` and `HttpUtility.UrlDecode`, and instead use `System.Uri.EscapeDataString` and `System.Uri.UnescapeDataString`.
affects: >=0.3.0
gotchaOlder versions of `cookies-js` (prior to v1.2.3) could encounter a null object reference error when both the global `this` and `window` objects were undefined, particularly in bundled environments like webpack.
fix
Upgrade `cookies-js` to version `1.2.3` or higher to resolve issues related to global context availability in modern bundling environments.
affects: <1.2.3
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'document')
Attempting to use `cookies-js` directly in a Node.js environment without providing a `window` object.
fix
Initialize `cookies-js` by providing a `window` instance from a library like `jsdom`: `const { JSDOM } = require('jsdom'); const window = new JSDOM().window; const Cookies = require('cookies-js')(window);`
Cookie values or keys are malformed/incorrect when read from the server.
Inconsistent URI encoding between client-side `cookies-js` (UTF-8) and server-side processing, often due to server frameworks using non-standard URL encoders/decoders.
fix
Verify that both client and server are using compatible URI encoding (UTF-8). For .NET, use `System.Uri.EscapeDataString` and `System.Uri.UnescapeDataString` instead of `HttpUtility` methods.
`Cookies.enabled` always returns `true` even when cookies are disabled in Internet Explorer 7 or 8.
A bug in `cookies-js` versions prior to `0.4.0` caused `Cookies.enabled` to incorrectly report `true` in older IE browsers.
fix
Upgrade `cookies-js` to version `0.4.0` or higher to fix the `Cookies.enabled` detection in IE7/IE8.
Upgrade
Version history
1.2.3latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
12 hits · last 30 days
node
12
Resources
cookies-js — npm install cookies-js · libregistry