我正在使用 Electron 来制作一个桌面应用程序。在我的应用程序中,我正在加载一个外部网站(Atom应用程序之外)比如说http://mydummysite/index.html页面。
这是在 Atom编辑器 中的我的应用程序结构:
即它包含以下部分:
main.js
package.json
nodemodules>jquery
(用于加载jquery)
源代码:
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"
}
}
外部页面 - http://mydummysite/index.html 页面代码:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>Hello World!</h1>
</body>
<script>
var jqr=require('jquery');
</script>
</html>
当我运行上述应用程序(将应用程序文件夹拖到Electron中)时,外部页面(http://mydummysite/index.html)在Electron shell中加载,但是出现以下错误:
你能帮我找出这个问题的原因吗?Uncaught Error: Cannot find module 'jquery'
如您在目录结构的截图中所见,我已经通过
npm install jquery
命令将jquery模块安装到我的文件夹中。注意:为了操作JS的
require
命令,我尝试在我的外部页面http://mydummysite/index.html中添加了require("ipc")
,它可以正常工作,那么require("jquery")
的原因是什么呢?我是否以正确的方式在Electron中添加了外部模块(jquery)?
我是否在
package.json
中缺少某些依赖项?这是我已经尝试过的:
-
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
这是module.js
中抛出错误的位置的截图:
有人能建议为什么require("ipc")
能工作而require("jquery")
不能吗?我的目标是在启用node-integration的情况下在Electron应用程序中使用jQuery。