11

Error in the console log: "Uncaught ReferenceError: module is not defined"
This is what is causing the error:

        <script src="https://cdn.jsdelivr.net/npm/js-cookie@rc/dist/js.cookie.min.js"></script>

enter image description here

Does this happen to anyone else on Chrome and firefox? Does anyone have a solution?

This wasn't happening before, but started today.

Yi Zong Kuang
  • 544
  • 1
  • 6
  • 17
  • 1
    `module` is node.js feature. It doesn't work in the browser. There are various technics to emulate loading `commonjs` modules but it's usually done with bundlers during build phase. – aleksxor Jul 14 '21 at 19:32
  • Oh, I am guessing since the CDN file used to work perfectly fine in the past, it's because probably the author changed the code to only work in node.js... Man that sucks. Thanks though – Yi Zong Kuang Jul 14 '21 at 19:42
  • 1
    We just fixed the exact same issue by loading `https://cdn.jsdelivr.net/npm/js-cookie@3.0.0-rc.1/dist/js.cookie.min.js`. We've previously been loading `js-cookie@rc`. HTH – fernandojmartin Jul 15 '21 at 17:33
  • The code comes from js-cookie (I have linked in the post if you want access to the code it self) – Yi Zong Kuang Jul 27 '21 at 00:56
  • 1
    @kissu but he wanted to show the error sign so it has to be posted as an image – I_love_vegetables Jul 27 '21 at 02:18
  • 1
    @I_love_vegetables The image is useful to show that it is throwing an error but the code itself still needs to be in the question. – TylerH Aug 05 '21 at 15:19
  • 1
    yep, but the codes in the image arent necessary to be posted in the question because it isnt the one causing the error right? it is the script tag which is already posted in the question (im just assuming it based on the answers) – I_love_vegetables Aug 05 '21 at 15:49

2 Answers2

12

So it turns out it's the js-cookie's most recent release that broke things.

I posted the issue on their github repo and got a work around, until they solve this issue in their latest release:

https://github.com/js-cookie/js-cookie/issues/698

Elte156 said that to use this earlier version for now, until the breaking change resolves:

<script src="https://cdn.jsdelivr.net/npm/js-cookie@3.0.0-rc.1/dist/js.cookie.min.js"></script>

I can confirm that this work around works for me.

Yi Zong Kuang
  • 544
  • 1
  • 6
  • 17
  • 1
    "until they solve this issue in their latest release" - I don't think this is correct. They have no "issue" on their side, they had a breaking change that will require modifications in the future. Pinning an old version will work for now, but don't expect that you can simply remove `3.0.0-rc.1` in the future (with any future release). See also https://github.com/js-cookie/js-cookie/issues/698#issuecomment-880466721 – bers Jul 15 '21 at 12:30
  • 1
    Yes you are right. And in the link you reference, it seems the author has added the correct link in their README.md for the UMD CDN release. – Yi Zong Kuang Jul 15 '21 at 16:14
2

Major releases of packages can shift around files. In this case the referenced file became a CommonJS module, which doesn't run directly in the browser. The good news is that many packages, including the new version of js-cookie, tell jsdelivr which file to use by default.

If you use this url, everything should keep working: https://cdn.jsdelivr.net/npm/js-cookie@rc

Rouven Weßling
  • 611
  • 4
  • 16