最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - $ is not defined - using jQuery with electron.js - Stack Overflow

programmeradmin1浏览0评论

I know there are many known solutions to this. But not a single one works. I keep getting this error:

Uncaught Exception:
ReferenceError: $ is not defined
    at: .....

Please note that I am using the npm installation of jQuery (npm install jquery --save).

I tried doing this:

<script> 
    window.nodeRequire = require; 
    delete window.require;
    delete window.exports; 
    delete window.module; 
    window.$ = window.jQuery = require('jquery');
</script>

but still got the error.

Then I tried:

<script>window.$ = window.jQuery = require('jquery');</script>

but this also did not work.

I tried it with a local file as well:

<script>window.$ = window.jQuery = require('./jquery-3.3.1.min.js');</script>

But once again, I get the dreaded "$ is not defined" error. Can someone please explain what is happening. I understand how these solutions should work but I don't understand why they do not work for me.

Here is my full HTML page in its current state:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>...</title>
  </head>
  <body>
    <h1>Test</h1>
    <div id = "test"></div>
    <script>window.$ = window.jQuery = require('./jquery-3.3.1.min.js'); 
   </script>

    <script>
      require('./renderer.js')
    </script>

  </body>
</html>

I started this project with the electron quick start at .

Thanks in advance.

Edit: Interestingly, the oauth library was interfering with jquery as well. But even after removing oauth, it still is broken.

I know there are many known solutions to this. But not a single one works. I keep getting this error:

Uncaught Exception:
ReferenceError: $ is not defined
    at: .....

Please note that I am using the npm installation of jQuery (npm install jquery --save).

I tried doing this:

<script> 
    window.nodeRequire = require; 
    delete window.require;
    delete window.exports; 
    delete window.module; 
    window.$ = window.jQuery = require('jquery');
</script>

but still got the error.

Then I tried:

<script>window.$ = window.jQuery = require('jquery');</script>

but this also did not work.

I tried it with a local file as well:

<script>window.$ = window.jQuery = require('./jquery-3.3.1.min.js');</script>

But once again, I get the dreaded "$ is not defined" error. Can someone please explain what is happening. I understand how these solutions should work but I don't understand why they do not work for me.

Here is my full HTML page in its current state:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>...</title>
  </head>
  <body>
    <h1>Test</h1>
    <div id = "test"></div>
    <script>window.$ = window.jQuery = require('./jquery-3.3.1.min.js'); 
   </script>

    <script>
      require('./renderer.js')
    </script>

  </body>
</html>

I started this project with the electron quick start at https://github./electron/electron-quick-start .

Thanks in advance.

Edit: Interestingly, the oauth library was interfering with jquery as well. But even after removing oauth, it still is broken.

Share Improve this question edited May 11, 2018 at 3:50 gkgkgkgk asked May 11, 2018 at 1:29 gkgkgkgkgkgkgkgk 7172 gold badges7 silver badges26 bronze badges 3
  • Did you check this one, stackoverflow./questions/32621988/… – highhope Commented May 11, 2018 at 1:46
  • @highhope i did, tried it, and didn't work. I even started a fresh project and it didn't work. Should I reinstall NPM or electron? I'm really not sure what to do. – gkgkgkgk Commented May 11, 2018 at 3:57
  • Interesting. I forked the quick-start example & I was able to get jQuery to work. My installed node version is v8.10.0. – Neil P. Commented May 11, 2018 at 4:26
Add a ment  | 

1 Answer 1

Reset to default 4

I've also done npm install jquery --save on my Electron project.

The way I have gotten jQuery to work is by requiring it in the <head> tag of my HTML before any of the other <script> tags.

<!DOCTYPE html>
<html lang="en">
  <head>
    ...
    <!-- jQuery (Should be declared before other JS imports) -->
    <script>
      var $ = jQuery = require("jquery")
    </script>
    ...
    <script src="../node_modules/jquery/dist/jquery.slim.min.js"></script>
    <script src="../node_modules/popper.js/dist/umd/popper.min.js"></script>
    <script src="../node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
    ...
  </head>
  <body>
    ....
  </body>
</html>

I needed to install jQuery for Bootstrap so I require it before declaring the script source import for jQuery, followed by other JS that I need.

EDIT After integrating electron-forge , the above solution no longer works for me. What does work is the solution mentioned here: https://stackoverflow./a/37480521/1392578

发布评论

评论列表(0)

  1. 暂无评论