电子 require() 未定义 [英] Electron require() is not defined
问题描述
我正在为自己的目的创建一个 Electron 应用程序.我的问题是,当我在 HTML 页面中使用节点函数时,它会引发以下错误:
I'm creating an Electron app for my own purpose. My problem is when I'm using node functions inside my HTML page it throws an error of:
'require()' 未定义.
'require()' is not defined.
有没有办法在我的所有 HTML 页面中使用 Node 功能?如果可能,请给我一个如何执行此操作的示例或提供链接.以下是我尝试在 HTML 页面中使用的变量:
Is there any way to use Node functionalities in all my HTML pages? If it is possible please give me an example of how to do this or provide a link. Here are the variables I'm trying to use in my HTML page:
var app = require('electron').remote;
var dialog = app.dialog;
var fs = require('fs');
这些是我在 Electron 中的所有 HTML 窗口中使用的值.
and these are the values I'm using in all my HTML windows within Electron.
推荐答案
从第 5 版开始,nodeIntegration
的默认值从 true 更改为 false.您可以在创建浏览器窗口时启用它:
As of version 5, the default for nodeIntegration
changed from true to false.
You can enable it when creating the Browser Window:
app.on('ready', () => {
mainWindow = new BrowserWindow({
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
}
});
});
这篇关于电子 require() 未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!