未捕获的错误:找不到模块“jquery" [英] Uncaught Error: Cannot find module 'jquery'

查看:46
本文介绍了未捕获的错误:找不到模块“jquery"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用

即它具有以下部分:

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

源代码:

main.js:

 '使用严格';var app = require('app');app.on('准备好', function() {var BrowserWindow = require('浏览器窗口');var 赢 =new BrowserWindow({ 宽度:800,高度:600,显示:假,'节点集成':真});win.on('关闭', function() {赢=空;});win.loadUrl('http://mydummysite/index.html');赢.秀();});

package.json:

<代码>{"name": "my-mac-app","版本": "5.2.0","description": "我的 Mac 桌面应用程序","main": "main.js",脚本":{开始":电子."},"作者": "我","许可证": "ISC",依赖关系":{jquery":^2.1.4"}}

外部页面 -

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

正如你在我的目录结构截图中看到的,我已经将 jquery 模块安装到我的文件夹中,我是通过 npm install jquery 命令完成的.

注意:为了在 JS 中使用 require 命令,我尝试在我的外部页面中添加 require("ipc")

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

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

解决方案

tl;dr

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


更新:

问题的答案

<块引用>

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

可在此问题中找到,指出系统/用户模块不应包含在全局模块路径

<块引用>

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

如果你看看 Electron 的源码 可以看到在module.globalPaths 中添加了内部模块:

# 添加 common/api/lib 到模块搜索路径.globalPaths.push path.resolve(__dirname, '..', 'api', 'lib')

这就是为什么您可以访问 ipcapp 等,但不能访问使用 npm install -g 全局安装的模块.


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

export NODE_PATH=/PATH/TO/APP/node_modules电子/PATH/TO/APP

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


**更新 2:**

评论的答案:

<块引用>

我收到未找到 jQuery 的错误

可以在这张票中找到.基本上,如果您使用 jQuery 的 npm 包或在您的 Electron 中的 HTML 文件中执行以下操作:

您得到的是一个工厂,而不是附加到全局上下文的实际 jQuery 对象(例如 window).正如我在上一个答案(也包含jQuery的源代码)

中提到的<块引用>

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

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


我写了一篇文章来解释Node + jQuery 的组合.

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

Here is the structure of my app in Atom Editor:

i.e it is having following parts:

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

Source code:

main.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:

{
  "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.html page code:

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

Uncaught Error: Cannot find module 'jquery'

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 jquery command.

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

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

Am I missing some dependency in 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

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

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

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

解决方案

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_modules
electron /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.

这篇关于未捕获的错误:找不到模块“jquery"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆