1

I'm writing an app using atom/electron which is entirely built from an existing html/css/js source.

After a few hours tweaking grunt scripts i finally got something building. However, I kept getting the following error:

Uncaught ReferenceError: jQuery is not defined

After some playing around and general bashing my head against a wall I finally realised that this clever little bit of the jQuery source was to blame.

Inparticular, this line

if ( typeof module === "object" && typeof module.exports === "object" ) {
    // Assume node context
}

So I'm not going insane. jQuery is in-fact loaded but it is bootstrapped to the node context instead of the browser context, causing all successive plugins to complain that window.jQuery is not defined.

My first thought was to throw in this cheeky snippet after the jquery source

if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
    global.jQuery = global.$ = window.jQuery = window.$ = module.exports;
}

This looks like it has done the trick. However, I'm no expert on atom/electron/webkit. Does anyone know of a cleaner way around this and whether changing the module.exports of a file included in a <script> tag is wise?

Cheers

StickyCube
  • 1,721
  • 2
  • 17
  • 22
  • possible duplicate of [Uncaught Error: Cannot find module 'jquery'](http://stackoverflow.com/questions/32265449/uncaught-error-cannot-find-module-jquery) – Yan Foto Sep 02 '15 at 19:04
  • This might help you: http://stackoverflow.com/a/32335874/2295964 – Yan Foto Sep 02 '15 at 19:05

1 Answers1

1

I also had the same problem. I don't know where I found that solution, but that worked fine for me.

If you want to use jQuery with electron you should require it in every html file as a module. Than jQuery works or other frameworks, which need jQuery.

<script>$ = jQuery = require("jquery")</script>

To install the jQuery module use npm

npm install --save jquery
apxp
  • 5,240
  • 4
  • 23
  • 43