How to rewrite jQuery Cookie, core/js-cookie library in Drupal with the js_cookie module
Why this change?
- In Drupal 9, jQuery Cookie was removed from core and replaced with the js-cookie library. A BC shim (
core/jquery.cookie) existed during Drupal 9 but was removed for Drupal 10.
https://www.drupal.org/node/3104677 - In Drupal 10.1, the
core/js-cookieasset library itself was deprecated and scheduled for removal in Drupal 11 because core no longer used it. The change record recommends switching to the contributed JS Cookie module.
https://www.drupal.org/node/3322720
https://www.drupal.org/project/drupal/issues/3296086 - The contributed JS Cookie module (
drupal/js_cookie) provides an asset library definition for js-cookie so modules/themes can continue to use Cookies in Drupal 10 and 11. https://www.drupal.org/project/js_cookie
Migration path overview
1) Drupal 8 → 9: jQuery Cookie → core/js-cookie
Historical context: Drupal 9 replaced jQuery Cookie with js-cookie and provided code and library mapping examples. https://www.drupal.org/node/3104677
# BEFORE (Drupal 8 / early 9)
my_library:
js:
js/my_library.js: {}
dependencies:
- core/jquery
- core/jquery.cookie
- core/drupal
// BEFORE (jQuery Cookie API)
(($, Drupal) => {
Drupal.behaviors.myModule = {
attach: () => {
$.cookie('cutest', 'red panda');
const myCookieValue = $.cookie('cutest');
$.removeCookie('cutest');
$.cookie.json = true;
$.cookie('cutest', { animal: 'red panda' });
},
};
})(jQuery, Drupal);
After (Drupal 9) you depended on core/js-cookie and used Cookies.* API: https://www.drupal.org/node/3104677
# AFTER (Drupal 9)
my_library:
js:
js/my_library.js: {}
dependencies:
- core/drupal
- core/js-cookie
// AFTER (Drupal 9, js-cookie API)
((Drupal, Cookies) => {
Drupal.behaviors.myModule = {
attach: () => {
Cookies.set('cutest', 'red panda');
const myCookieValue = Cookies.get('cutest');
Cookies.remove('cutest');
Cookies.set('cutest', JSON.stringify({ animal: 'red panda' }));
const cutest = JSON.parse(Cookies.get('cutest') || 'null');
},
};
})(Drupal, window.Cookies);
2) Drupal 10.1+ and 11: core/js-cookie → contributed js_cookie
Since Drupal core deprecated core/js-cookie in 10.1 and removed it in 11, you must replace it with the contrib JS Cookie module’s library: js_cookie/js-cookie. https://www.drupal.org/node/3322720
Composer
composer require drupal/js_cookie
This installs the module that exposes a Drupal asset library for the upstream js-cookie package. https://www.drupal.org/project/js_cookie
Declare a module/theme dependency (for contrib/custom projects)
# my_module.info.yml (or your theme's .info.yml)
name: My Module
type: module
core_version_requirement: ^10 || ^11
dependencies:
- js_cookie:js_cookie
The project page explicitly notes adding js_cookie:js_cookie as a dependency for contrib modules (and to require drupal/js_cookie in composer if your module has its own composer.json). [4](https://www.drupal.org/project/js_cookie)
Switch your asset library dependency
# Replace this (deprecated in 10.1, removed in 11)
# - core/js-cookie
# With this (provided by the contrib module)
my_library:
js:
js/my_library.js: {}
dependencies:
- core/drupal
- js_cookie/js-cookie
This exact replacement is the path recommended in the core change record announcing the deprecation. [2](https://www.drupal.org/node/3322720)
JavaScript code
Your JS code does not need changes if you already used the Cookies API from js-cookie—only the library dependency changes. [2](https://www.drupal.org/node/3322720)
((Drupal, Cookies) => {
Drupal.behaviors.myModule = {
attach: () => {
// Set a cookie.
Cookies.set('cutest', 'red panda', { path: '/', sameSite: 'Lax' });
// Get a cookie.
const myCookieValue = Cookies.get('cutest');
// Remove a cookie.
Cookies.remove('cutest', { path: '/' });
// Store/retrieve JSON safely.
Cookies.set('cutest', JSON.stringify({ animal: 'red panda' }));
const cutestRaw = Cookies.get('cutest');
const cutest = cutestRaw ? JSON.parse(cutestRaw) : null;
},
};
})(Drupal, window.Cookies);
Tip: js-cookie is RFC 6265–compliant and encodes values differently than the old jQuery Cookie, especially for JSON. The Drupal 9 change record outlines these behavior differences. https://www.drupal.org/node/3104677
Examples from the ecosystem
- The EU Cookie Compliance module tracked the deprecation of
core/js-cookieand patches were proposed to switch tojs_cookie/js-cookie. https://www.drupal.org/project/eu_cookie_compliance/issues/3380490 - Various contrib projects filed issues to replace jQuery Cookie with js-cookie and later to address the deprecation of
core/js-cookie. https://www.drupal.org/project/quicktabs/issues/3298649
https://www.drupal.org/project/fpa/issues/3505122
Step-by-step upgrade recipes
A) From jQuery Cookie directly to js_cookie/js-cookie (Drupal 10+)
- Remove
core/jqueryandcore/jquery.cookiedependencies from your.libraries.yml. https://www.drupal.org/node/3104677 - Add
js_cookie:js_cookieto your.info.ymldependencies (for contrib/custom modules and themes). https://www.drupal.org/project/js_cookie - Change your library dependency to
js_cookie/js-cookie. https://www.drupal.org/project/js_cookie - Rewrite JS from
$.cookietoCookies.*as shown above. https://www.drupal.org/node/3104677
B) From core/js-cookie to js_cookie/js-cookie (Drupal 10.1 → 11)
- Require the module:
composer require drupal/js_cookie. https://www.drupal.org/project/js_cookie - (Contrib) Add
js_cookie:js_cookiein.info.yml; just enable the module https://www.drupal.org/project/js_cookie. - In
.libraries.yml, replacecore/js-cookiewithjs_cookie/js-cookie. https://www.drupal.org/node/3322720 - Keep your existing
Cookies.*JS code. No functional changes needed. https://www.drupal.org/node/3322720
Gotchas and best practices
- Drupal 11 readiness:
core/js-cookieis removed in Drupal 11 - ensure all dependencies point tojs_cookie/js-cookie. https://www.drupal.org/node/3322720 - Privacy/CDN: The js_cookie project page documents how to avoid loading js-cookie from a CDN (data protection). Prefer local assets. https://www.drupal.org/project/js_cookie
- Encoding & JSON: Don’t rely on implicit JSON behavior. Explicitly
JSON.stringifyandJSON.parseas shown earlier. https://www.drupal.org/node/3104677 - Real-world patches: Review issue queues (e.g., EU Cookie Compliance) for patterns when updating. https://www.drupal.org/project/eu_cookie_compliance/issues/3380490