Problem: while developing using Electron, when you try to use any JS plugin that requires jQuery, the plugin doesn't find jQuery, even if you load in the correct path using script tags.
问题:
回答1:
A better an more generic solution IMO:
Benefits
- Works for both browser and electron with the same code
- Fixes issues for ALL 3rd-party libraries (not just jQuery) without having to specify each one
- Script Build / Pack Friendly (i.e. Grunt / Gulp all scripts into vendor.js)
- Does NOT require
node-integration
to be false
source here
回答2:
As seen in https://github.com/atom/electron/issues/254 the problem is caused because of this code:
if ( typeof module === "object" && typeof module.exports === "object" ) { // set jQuery in `module` } else { // set jQuery in `window` }
The jQuery code "sees" that its running in a CommonJS environment and ignores window
.
The solution is really easy, instead of loading jQuery through , you should load like this:
Note: the dot before the path is required, since it indicates that it's the current directory. Also, remember to load jQuery before loading any other plugin that depends on it.
回答3:
Another way of writing is :
回答4:
electron FAQ answer :
http://electron.atom.io/docs/faq/
I can not use jQuery/RequireJS/Meteor/AngularJS in Electron.
Due to the Node.js integration of Electron, there are some extra symbols inserted into the DOM like module, exports, require. This causes problems for some libraries since they want to insert the symbols with the same names.
To solve this, you can turn off node integration in Electron:
// In the main process.
let win = new BrowserWindow({ webPreferences: { nodeIntegration: false } });
But if you want to keep the abilities of using Node.js and Electron APIs, you have to rename the symbols in the page before including other libraries:
回答5:
Just came across this same problem
npm install jquery --save
worked for me
回答6:
You can put node-integration: false
inside options on BrowserWindow.
eg: window = new BrowserWindow({'node-integration': false});
回答7:
A nice and clean solution
- Install jQuery using npm. (
npm install jquery --save
) - Use it:
回答8:
I think i understand your struggle i solved it little bit differently.I used script loader for my js file which is including jquery.Script loader takes your js file and attaching it to top of your vendor.js file it did the magic for me.
https://www.npmjs.com/package/script-loader
after installing the script loader add this into your boot or application file.
import 'script!path/your-file.js';
回答9:
ok, here's another option, if you want a relative include...
回答10:
before your jquery import and you're good to go. more info here.