Nat*_*end 9 require node.js node-modules electron
通常,在Electron应用程序中,您可以require从主进程和渲染器进程中节点模块:
var myModule = require('my-module');
Run Code Online (Sandbox Code Playgroud)
但是,如果页面是通过HTTP而不是本地文件系统加载的,那么这似乎不起作用.换句话说,如果我打开这样一个窗口:
win.loadURL(`file://${__dirname}/index.html`);
Run Code Online (Sandbox Code Playgroud)
我可以require没有问题的节点模块.但是,如果我打开一个这样的窗口:
win.loadURL(`http://localhost:1234/index.html`);
Run Code Online (Sandbox Code Playgroud)
我的require网页中不再有节点模块 - 我进入Uncaught Error: Cannot find module 'my-module'了网页的控制台.有没有办法在通过HTTP提供的Electron页面中使用节点模块?
一点上下文:我的公司正在构建一个需要能够作为Web应用程序托管并在Electron shell中的应用程序.为了使这两个环境更加简单和一致,我的Electron应用程序启动了一个本地Web服务器并打开了托管的应用程序http://localhost:1234.现在,我希望能够使用拼写检查/拼写建议添加到应用程序中electron-spell-check-provider.这个模块需要在渲染器进程中导入和初始化,所以我正在尝试require('electron-spell-check-provider')进入我的网页,但是这会因Cannot find module错误而失败.
ndm*_*web -2
遇到了类似的问题。尝试在你的index.html中通过HTTP提供renderer.js ,如下所示,
<script src="/renderer.js"></script>
</body>
Run Code Online (Sandbox Code Playgroud)
然后,根据文档,在renderer.js文件中的 require 之后使用添加远程加载模块。
var spellCheck = require('electron-spell-check-provider').remote;
| 归档时间: |
|
| 查看次数: |
5237 次 |
| 最近记录: |