For those operating in a Browser-Only environment, the recommended solution is to utilize the one provided by SridharR.
However, if you find yourself in a Node/CommonJS + Browser environment (such as electron or node-webkit), you may encounter an error due to jQuery's logic for exporting, which first checks for module
over window
:
if (typeof module === "object" && typeof module.exports === "object") {
// CommonJS/Node
} else {
// window
}
Keep in mind that in this scenario, jQuery exports itself through module.exports
; hence, jQuery
and $
are not automatically assigned to window
.
To address this issue, instead of using
<script src="path/to/jquery.js"></script>
,
You can manually assign it through a require
statement:
<script>
window.jQuery = window.$ = require('jquery');
</script>
NOTE: If your electron application does not necessitate nodeIntegration
, consider setting it to false
to avoid the need for this workaround.