在 Electron HTML 页面中未定义 require()

2022-01-10 00:00:00 node.js electron javascript webview

我目前无法在 main.js 文件之外的 Javascript 文件中加载 Electron 模块.

I'm currently unable to load Electron modules in Javascript files outside of the main.js file.

我正在尝试使用:

const electron = require('electron');

const ipcRenderer = electron.ipcRenderer;

在名为 settings.js 的文件中.

in a file called settings.js.

我安装了正确的 NPM 模块(eletron-prebuilt).

I have the correct NPM modules installed (eletron-prebuilt).

设置页面在 webview 中加载,如下所示:

The settings page is loaded in a webview like so:

<webview id="settings" src="./settings.html"></webview>

其中包括的 settings.js 如下:

and the settings.js included like:

<script type='text/javascript' src='./resources/js/settings.js'></script>

这里的想法用完了,但我想知道这是否是某种范围界定问题?

Running out of ideas here, but I wonder if it's some kind of scoping issue?

非常感谢.

推荐答案

你在一个不支持 nodeintegration 默认.

You're loading it in a webview, which doesn't support nodeintegration by default.

nodeintegration 属性添加到您的 webivew.

Add the nodeintegration attribute to your webivew.

<webview id="settings" src="./settings.html" nodeintegration></webview>

相关文章