Electron: jQuery is not defined

前端 未结 19 2458
我在风中等你
我在风中等你 2020-11-22 06:20

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

相关标签:
19条回答
  • 2020-11-22 06:54

    A nice and clean solution

    1. Install jQuery using npm. (npm install jquery --save)
    2. Use it: <script> let $ = require("jquery") </script>
    0 讨论(0)
  • 2020-11-22 06:54

    For this issue, Use JQuery and other js same as you are using in Web Page. However, Electron has node integration so it will not find your js objects. You have to set module = undefined until your js objects are loaded properly. See below example

    <!-- Insert this line above local script tags  -->
    <script>if (typeof module === 'object') {window.module = module; module = 
    undefined;}</script>
    
    <!-- normal script imports etc  -->
    <script
      src="https://code.jquery.com/jquery-3.4.1.min.js"
      integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
      crossorigin="anonymous"></script>    
    
    <!-- Insert this line after script tags -->
    <script>if (window.module) module = window.module;</script>
    

    After importing like given, you will be able to use the JQuery and other things in electron.

    0 讨论(0)
  • 2020-11-22 06:55

    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:

    <head> 
    <script> 
    window.nodeRequire = require; 
    delete window.require;
    delete window.exports; delete window.module; 
    </script> 
    <script type="text/javascript" src="jquery.js"></script> 
    </head>
    
    0 讨论(0)
  • 2020-11-22 07:00

    worked for me using the following code

    var $ = require('jquery')
    
    0 讨论(0)
  • 2020-11-22 07:01

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

    0 讨论(0)
  • 2020-11-22 07:02

    you may try the following code:

    mainWindow = new BrowserWindow({
            webPreferences: {
                nodeIntegration:false,
            }
    });
    
    0 讨论(0)
提交回复
热议问题