Skip to content

jQuery isn't set globally because "module" is defined #254

Closed

Description

@luto

jQuery contains something along this lines:

if ( typeof module === "object" && typeof module.exports === "object" ) {
  // set jQuery in `module`
} else {
  // set jQuery in `window`
}

module is defined, even in the browser-side scripts. This causes jQuery to ignore the window object and use module, so the other scripts won't find $ nor jQuery in global scope..

I am not sure if this is a jQuery or atom-shell bug, but I wanted to put this on the web, so others won't search as long as I did.

Activity

  1. Twipped commented on May 6, 2014

    @Twipped

    This isn't really a bug for either system, you have the same problem when using browserify. jQuery sees that its running in a CommonJS environment and expects to be used as such.

    The solution is relatively simple, however. Instead of loading jQuery as a script tag, load it via require:

    window.$ = window.jQuery = require('/path/to/jquery');
  2. zcbenz commented on May 7, 2014

    @zcbenz
    Contributor

    I agree with @ChiperSoft, this is expected behavior in CommonJS environment, so I marked this as wontfix. And thanks for sharing your finding.

  3. alexdong commented on Jun 7, 2014

    @alexdong

    @ChiperSoft, Where should I put the jquery.js file to get require('jquery') working? I've tried to put it at the same level as package.js and main.js but the runtime doesn't seem to pick it up.

  4. JoshTheDerf commented on Jun 7, 2014

    @JoshTheDerf

    You can use require('jquery.js') note the .js at the end, to load jquery relative to your HTML file.

  5. added a commit that references this issue on Aug 31, 2014
  6. mpcode commented on Sep 28, 2014

    @mpcode

    Sorry, I used window.$ = window.jQuery = require('/path/to/jquery'); as mentioned but it doesn't work,
    Uncaught Error: Cannot find module 'scripts/jquery-1.10.2.min.js' module.js:339

  7. frankhale commented on Sep 28, 2014

    @frankhale
    Contributor

    I use that all the time and it works great! Here is what I use, be mindful about the path:

    window.$ = window.jQuery = require('./scripts/jquery-2.1.1.min.js');
  8. mpcode commented on Sep 28, 2014

    @mpcode

    @frankhale oh, it worked with ./, thanks!

  9. frankhale commented on Sep 28, 2014

    @frankhale
    Contributor

    Awesome! The path thing tripped me up as well.

  10. kabalage commented on Mar 17, 2015

    @kabalage

    If your app does not need node-integration, add "node-integration": false to your BrowserWindow options. In this case module.exports is not available, jQuery works like expected.

  11. miguelmota commented on Apr 27, 2015

    @miguelmota

    @kabalage worked wonderfully, thanks!

  12. vrunoa commented on May 6, 2015

    @vrunoa

    👍

  13. 56 remaining items

  14. Bronts commented on Oct 28, 2016

    @Bronts

    I resolved this problem with this

    <script type="text/javascript" src="js/jquery.min.js" onload="window.$ = window.jQuery;"></script>
    

    and set

    webPreferences: {
                nodeIntegration: false, 
            }
    

    if you with nodeIntegration: true, imorot jQuery like this

    <script type="text/javascript" src="js/jquery.min.js" onload="window.$ = window.jQuery = module.exports;"></script>
    

    reference to
    #345

  15. drowlands commented on Nov 2, 2016

    @drowlands

    I mean, it seems to be a bad problem, so why not include it in actual Electron? Maybe integrate major JavaScript libraries into Electron?

  16. reneroth commented on Nov 3, 2016

    @reneroth

    Chances are, if you are using jQuery in an Electron App, you are doing something very wrong or at least inefficient.

  17. amhoho commented on Jan 13, 2017

    @amhoho
    <script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
    <script>if (typeof module === 'object') {window.jQuery = window.$ = module.exports;};</script>
    
  18. englishextra commented on Jan 13, 2017

    @englishextra

    @drowlands Including 3party libs in Electron seems to be a bad idea. Imagine the update routine, for instance, or chosing a version.
    @amhoho That's because you need to require jQuery, or modify its init code at the top function - leaving only factory( global );

    Here:

    ( function( global, factory ) {
    
    	"use strict";
    
    	if ( typeof module === "object" && typeof module.exports === "object" ) {
    
    		// For CommonJS and CommonJS-like environments where a proper `window`
    		// is present, execute the factory and get jQuery.
    		// For environments that do not have a `window` with a `document`
    		// (such as Node.js), expose a factory as module.exports.
    		// This accentuates the need for the creation of a real `window`.
    		// e.g. var jQuery = require("jquery")(window);
    		// See ticket #14549 for more info.
    		module.exports = global.document ?
    			factory( global, true ) :
    			function( w ) {
    				if ( !w.document ) {
    					throw new Error( "jQuery requires a window with a document" );
    				}
    				return factory( w );
    			};
    	} else {
    		factory( global );
    	}
    

    I my case I load libs via fetch/Promise or xhr, so I have this routine to remove module checks in vendors' libs.
    @RROrg weird what you're saying

  19. anuxs commented on Mar 26, 2017

    @anuxs

    mark

  20. karlhorky commented on Apr 5, 2017

    @karlhorky

    @kabalage I think the configuration in your post above is out of date. It looks like it should be like this now:

    const window = new BrowserWindow({
      webPreferences: {
        nodeIntegration: false, 
      },
    });
  21. added 2 commits that reference this issue on May 9, 2017
  22. souzace commented on May 12, 2018

    @souzace

    On official docs:
    https://electronjs.org/docs/faq#i-can-not-use-jqueryrequirejsmeteorangularjs-in-electron

    <head>
      <script>
      window.nodeRequire = require;
      delete window.require;
      delete window.exports;
      delete window.module;
      </script>
      <script type="text/javascript" src="jquery.js"></script>
    </head>
    
  23. alotex commented on Feb 21, 2019

    @alotex

    I'm getting
    TypeError: Cannot read property 'createElement' of undefined
    in webview.

    preload script is

    const path = require('path');
    const {ipcRenderer} = require('electron')
    const fs = require('fs');
    window.$ = window.jQuery = require(path.join(__dirname, '../jquery.min.js'))
    
  24. omprabhakaran commented on Sep 12, 2019

    @omprabhakaran

    if i use this code, it works fine,
    but can't able to close the browserwindow by click of top right button.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions