Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firefox and Chrome error when linking js-cookie: Uncaught ReferenceError: module is not defined [closed]

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.

like image 1000
Yi Zong Kuang Avatar asked Jul 14 '21 19:07

Yi Zong Kuang


People also ask

How do I fix uncaught ReferenceError?

Answer: Execute Code after jQuery Library has Loaded The most common reason behind the error "Uncaught ReferenceError: $ is not defined" is executing the jQuery code before the jQuery library file has loaded. Therefore make sure that you're executing the jQuery code only after jQuery library file has finished loading.

What is JavaScript ReferenceError?

The ReferenceError object represents an error when a variable that doesn't exist (or hasn't yet been initialized) in the current scope is referenced. ReferenceError is a serializable object, so it can be cloned with structuredClone() or copied between Workers using postMessage() .

Is not defined JavaScript error?

A not defined error is when we did not declare the variable and tried to call that variable. In JavaScript, we can declare variables without adding const , let , or var , and we won't get an error of undefined or not defined . This can be seen in the code below.

Is not defined in JavaScript file?

To solve the"ReferenceError: document is not defined" error, make sure to only use the document global variable on the browser. The variable relates to the Document Object Model, which represents a web page that is loaded in the browser and can't be used on the server side (e.g. in Node. js).


2 Answers

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/[email protected]/dist/js.cookie.min.js"></script>

I can confirm that this work around works for me.

like image 191
Yi Zong Kuang Avatar answered Sep 22 '22 15:09

Yi Zong Kuang


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

like image 42
Rouven Weßling Avatar answered Sep 20 '22 15:09

Rouven Weßling