Javascript 未捕获的错误:找不到模块“jquery”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32265449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:49:33  来源:igfitidea点击:

Uncaught Error: Cannot find module 'jquery'

javascriptjquerynode.jsatom-editorelectron

提问by Raghav

I am using Electronto make a desktop app. In my app I am loading a an external site (outside Atom app) lets say http://mydummysite/index.htmlpage.

我正在使用Electron制作桌面应用程序。在我的应用程序中,我正在加载一个外部站点(在 Atom 应用程序之外)可以说http://mydummysite/index.html页面。

Here is the structure of my app in Atom Editor:

这是Atom Editor中我的应用程序的结构:

enter image description here

在此处输入图片说明

i.e it is having following parts:

即它有以下部分:

  1. main.js
  2. package.json
  3. nodemodules>jquery(to load jquery)
  1. main.js
  2. package.json
  3. nodemodules>jquery(加载jquery)

Source code:

源代码:

main.js:

主要.js:

   'use strict';

    var app = require('app');

    app.on('ready', function() {
      var BrowserWindow = require('browser-window');

      var win = 
      new BrowserWindow({ width: 800, height: 600, show: false, 
               'node-integration':true });
      win.on('closed', function() {
        win = null;
      });

      win.loadUrl('http://mydummysite/index.html ');
      win.show();
    });

package.json:

包.json:

{
  "name": "my-mac-app",
  "version": "5.2.0",
  "description": "My Mac Desktop App",
  "main": "main.js",
  "scripts": {
    "start": "electron ."
  },
  "author": "Me",
  "license": "ISC",
  "dependencies": {
    "jquery": "^2.1.4"
  }
}

External page - http://mydummysite/index.htmlpage code:

外部页面 - http://mydummysite/index.html页面代码:

<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
    <h1>Hello World!</h1>

  </body>
<script>

   var jqr=require('jquery');

</script>
</html>

When I run the above app (by dragging the application folder to Electron) the external page (http://mydummysite/index.html) loads in Electron shell but with the error

当我运行上述应用程序(通过将应用程序文件夹拖到 Electron)时,外部页面(http://mydummysite/index.html)加载到 Electron shell 中,但出现错误

Uncaught Error: Cannot find module 'jquery'

未捕获的错误:找不到模块“jquery”

enter image description here

在此处输入图片说明

Can you help me finding the cause of this issue?

你能帮我找到这个问题的原因吗?

As you can see in my screenshot of directory structure I have alread installed jquery module to my folder and I did it via npm install jquerycommand.

正如您在我的目录结构屏幕截图中所看到的,我已经将 jquery 模块安装到我的文件夹中,并且是通过npm install jquery命令完成的。

Note: To play with requirecommand in JS I tried adding require("ipc")in my external page http://mydummysite/index.htmlpage and it was working so what could be the reason with require("jquery").

注意:为了require在 JS 中使用命令,我尝试require("ipc")在我的外部页面http://mydummysite/index.html页面中添加它并且它正在工作,所以可能是require("jquery").

Did I add external module (jquery) in correct way in Electron?

我是否在 Electron 中以正确的方式添加了外部模块 (jquery)?

Am I missing some dependency in package.json?

我错过了一些依赖package.json吗?

What I have already tried:

我已经尝试过的:

  • npm cache clean, npm install jquery(to my app folder)
  • npm install --save jquery
  • npm install jquery -g
  • npm rebuild
  • sudo npm install jquery -g
  • sudo npm install jquery
  • export NODE_PATH=/usr/local/lib/node_modules
  • npm cache clean, npm install jquery(到我的应用程序文件夹)
  • npm install --save jquery
  • npm install jquery -g
  • npm rebuild
  • sudo npm install jquery -g
  • sudo npm install jquery
  • export NODE_PATH=/usr/local/lib/node_modules

Here is the screenshot of the location from where the error is thrown in module.js

这是错误所在位置的屏幕截图 module.js

enter image description here

在此处输入图片说明

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

有人可以建议为什么require("ipc")有效而require("jquery")不是?

My goal is to use jQuery with electron app with node-integration true.

我的目标是将 jQuery 与电子应用程序一起使用,节点集成为 true。

回答by Yan Foto

tl;dr

tl;博士

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_PATHenvironment variables. You have to set it manually to all the paths containing your desired modules.

与普通的 nodejs 应用程序相比,您可以访问全局模块(例如位于/usr/bin/node),电子不会自动设置NODE_PATH环境变量。您必须手动将其设置为包含所需模块的所有路径。



Update:

更新:

The answer to the question

问题的答案

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

为什么require("ipc")工作而require("jquery")不是?

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.

因为它们可能包含应用程序未附带的模块,并且可能使用错误的 v8 头文件编译。

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

如果您查看electron 的源代码,您会看到内部模块已添加到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.

这就是为什么你必须访问ipcapp等,但不模块您在使用已安装在全球范围npm install -g



I just tried it out with the latest electron-prebuiltversion 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_modulesdirectory under your app root to the NODE_PATHvariable it is not going to work. So you need to do something like this:

我刚刚尝试了最新electron-prebuilt版本的本地服务器,该服务器提供的 HTML 文件与您提供的完全相同,我想我知道问题所在:如果您没有将应用node_modules程序根目录下的应用程序目录的路径附加到NODE_PATH变量它不会工作。所以你需要做这样的事情:

export NODE_PATH=/PATH/TO/APP/node_modules
electron /PATH/TO/APP

When exporting NODE_PATHmake sure that you provide an absolute path.

导出时NODE_PATH,请确保提供绝对路径。



Update 2:更新 2:

The answer to the comment:

评论的答案:

I get jQuery not found errors

我得到 jQuery not found 错误

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:

是在这张票中找到的。基本上,如果您使用 jQuery 的 npm 包或在电子内部的 HTML 文件中执行以下操作:

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

What you get is a factoryand 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)

您得到的是一个工厂,而不是附加到全局上下文(例如window)的实际 jQuery 对象。正如我在之前的回答中提到的(还包含 jQuery 的源代码)

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

当您在 CommonJS 或提供moduleand 的类似环境中需要 jQuery 时module.exports,您得到的是一个工厂,而不是实际的 jQuery 对象。

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:

现在要使用该工厂(通过从 CDN 导入代码,或者如果您有本地可用的 npm 模块),您需要如下内容:

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


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

我写过一篇文章,解释了 Node + jQuery 的组合。

回答by Damien

Install jquery with npm isn't enough :

使用 npm 安装 jquery 是不够的:

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 :

它会恢复您项目中的 jQuery 源文件。但是你必须在你的 html 文件中包含脚本:

<!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>

回答by gunivan

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

我在将 jQuery 与电子一起使用时遇到了同样的问题,并为这些情况找到了解决方案:

<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

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

回答by VanagaS

# assuming you have installed jquery locally instead of globally like in as
npm install jquery -s         # without -g flag

instead of require("jquery"), give relative path from source directory
require("./node_modules/jquery/dist/jquery.min.js");

而不是require("jquery" ),从源目录中给出相对路径
require("./node_modules/jquery/dist/jquery.min.js" );

Try the following:

请尝试以下操作:

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

OR

或者

<script>var $ = jQuery = require('./node_modules/jquery/dist/jquery.min.js');</script>

回答by Akki619

I hope below link will put some light on your doubt for

我希望下面的链接可以解决您的疑问

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

为什么 require("ipc") 有效而 require("jquery") 无效?

https://github.com/atom/electron/issues/254

https://github.com/atom/electron/issues/254

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

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

回答by ProllyGeek

The same issue happened to me , a simple solution is to add this to your index.js file :

同样的问题发生在我身上,一个简单的解决方案是将其添加到您的 index.js 文件中:

app.on('ready', function() {
      var mainWindow = new BrowserWindow({
        "node-integration": false
      })
//rest of your initialization code here.
})

the issue is caused by node , for more information please refer to this post

该问题是由节点引起的,有关更多信息,请参阅此帖子

Setting node-integration to false will disable node.js in the renderer process - i.e. your app can only do what a web browser will do.

将 node-integration 设置为 false 将禁用渲染器进程中的 node.js - 即您的应用程序只能执行 Web 浏览器将执行的操作。