Electron 'require()' undefined: Enabling Node Access in HTML
在 Electron 中,如果遇到错误“require() is当尝试在 HTML 页面中使用 Node.js 功能时,“未定义”,这意味着从 Electron 版本 5 开始,Node 集成默认被禁用。要解决此问题,您必须为每个 BrowserWindow 显式启用 nodeIntegration。
To激活Node集成,修改BrowserWindow创建代码如下:
app.on('ready', () => {
mainWindow = new BrowserWindow({
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
}
});
});
通过将 nodeIntegration 设置为 true 并将 contextIsolation 设置为 false,您可以直接访问 Node.js 模块,从而使您能够利用以下变量:
var app = require('electron').remote;
var dialog = app.dialog;
var fs = require('fs');
在你的 HTML 页面和任何 Electron 窗口中。这使您可以在整个 Electron 应用程序中无缝地利用 Node.js 功能。
免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。
Copyright© 2022 湘ICP备2022001581号-3