Uncaught Error: Cannot find module 'jquery' Uncaught Error: Cannot find module 'jquery' javascript javascript

Uncaught Error: Cannot find module 'jquery'


tl;dr

In contrast to a normal nodejs app, where you have access to global modules (e.g. located in /usr/bin/node), electron doesn't automatically set the NODE_PATH environment variables. You have to set it manually to all the paths containing your desired modules.


Update:

The answer to the question

why require("ipc") is working and require("jquery") not?

is to be found in this issue, stating that system/user modules should not be included in the global path of the module

since they could contain modules not shipped with the app and possibly compiled with the wrong v8 headers.

And if you take a look at electron's source you can see that internal modules are added to the module.globalPaths:

# Add common/api/lib to module search paths.globalPaths.push path.resolve(__dirname, '..', 'api', 'lib')

thats why you have access to ipc, app, etc. but not the modules that you have installed globally using npm install -g.


I just tried it out with the latest electron-prebuilt version with a local server serving exactly the same HTML file that you provided and I think I know what the problem is: If you don't append the path to your app node_modules directory under your app root to the NODE_PATH variable it is not going to work. So you need to do something like this:

export NODE_PATH=/PATH/TO/APP/node_moduleselectron /PATH/TO/APP

When exporting NODE_PATH make sure that you provide an absolute path.


**Update 2:**

The answer to the comment:

I get jQuery not found errors

Is to be found in this ticket. Basically if you use the jQuery's npm package or do something like the following in your HTML files inside electron:

<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>

What you get is a factory and not the actual jQuery object attached to the global context (e.g window). As I mentioned in a previous answer (containing also jQuery's source code)

When you require jQuery inside a CommonJS or similar environment which provides module and module.exports, what you get is a factory and not the actual jQuery object.

Now to use that factory (either by importing the code from the CDN or if you have the npm module available locally) you would need something as the following:

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

I have written an article that explains the combination of Node + jQuery.


Install jquery with npm isn't enough :

npm install --save jquery

It recovers the source files of jQuery in your project. But you have to include the script in your html file :

<!DOCTYPE html><html>  <head></head>  <body>      <h1>Hello World!</h1>  </body>  <!-- Try to load from cdn to exclude path issues. -->  <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>  <script>     window.jQuery = window.$ = jQuery;     $(document).ready(function() {         console.log( "jQuery is loaded" );     });  </script></html>


I have the same issue when using jQuery with electron, and find out a solution for these case:

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

Source: https://discuss.atom.io/t/electron-app-to-host-external-site/16390/9