Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
luto opened this issue May 6, 2014 · 71 comments
Closed

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

luto opened this issue May 6, 2014 · 71 comments

Comments

@luto
Copy link

luto commented May 6, 2014

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.

@Twipped
Copy link

Twipped commented May 6, 2014

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');

@zcbenz zcbenz added the wontfix label May 7, 2014
@zcbenz
Copy link
Member

zcbenz commented May 7, 2014

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

@alexdong
Copy link

alexdong commented Jun 7, 2014

@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.

@JoshTheDerf
Copy link

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

kurtharriger pushed a commit to kurtharriger/gorilla-web that referenced this issue Aug 31, 2014
Atom shell appears to blow up when jquery is added as script tag.
electron/electron#254

Use commonjs syntax to export to window
@mpcode
Copy link

mpcode commented Sep 28, 2014

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

@frankhale
Copy link
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');

@mpcode
Copy link

mpcode commented Sep 28, 2014

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

@frankhale
Copy link
Contributor

Awesome! The path thing tripped me up as well.

cobalamin added a commit to cobalamin/dripdownode that referenced this issue Oct 5, 2014
Stop depending on jQuery and Vex, because electron/electron#254
is just ridiculous when loading non-file resources (__dirname is not set properly and process.cwd() is not usable)
@kabalage
Copy link

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.

@miguelmota
Copy link

@kabalage worked wonderfully, thanks!

@vrunoa
Copy link

vrunoa commented May 6, 2015

👍

@RamonGebben
Copy link

Walked up to the same issue. Install jQuery via NPM then required it and it worked.

@skandasoft
Copy link

what if node integeration and jquery without the require is needed...

@dleroux
Copy link

dleroux commented May 21, 2015

I'm trying to make sense of this since it appears to be something I could use. However I wonder when can you be sure you can safely use $. Logging $ right after this line will result would be require and not jquery since require is asynchronus?

window.$ = window.jQuery = require('/path/to/jquery');
console.log($) // logs 'require' not 'jQuery'

@RamonGebben
Copy link

@dieroux try installing it via npm:

npm install jquery

This worked for me while trying the same. Then you don't need to give a path, you just say:

window.$ = window.jQuery = require('jquery');
console.log( [$, jQuery ]);

devartblake added a commit to devartblake/Project_Hathor that referenced this issue Jun 29, 2015
Fixed an error with jQuery not running properly with Twitter bootstrap
and the Electron framework.
Reference: electron/electron#254
@vikbez
Copy link

vikbez commented Jul 16, 2015

What if we need node-integration, and we can't use require('something') ?
Currently it works by doing "delete module;" before loading problematic-stuff.
What's the impact of doing this ?

@lmj0011
Copy link

lmj0011 commented Oct 26, 2016

for those using the Aurelia framework:
aurelia/skeleton-navigation@6292e69

@Bronts
Copy link

Bronts commented Oct 28, 2016

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

@drowlands
Copy link

drowlands commented Nov 2, 2016

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

@reneroth
Copy link

reneroth commented Nov 3, 2016

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

@amhoho
Copy link

amhoho commented Jan 13, 2017

<script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
<script>if (typeof module === 'object') {window.jQuery = window.$ = module.exports;};</script>

@englishextra
Copy link

englishextra commented Jan 13, 2017

@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

@anuxs
Copy link

anuxs commented Mar 26, 2017

mark

@karlhorky
Copy link

karlhorky commented Apr 5, 2017

@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, 
  },
});

@souzace
Copy link

souzace commented May 12, 2018

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>

@alotex
Copy link

alotex commented Feb 21, 2019

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'))

@omprabhakaran
Copy link

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests