Javascript 电子5.0.0“;未捕获引用错误:未定义require“;
我最初使用的是electron stable(4.x.x),能够在浏览器和渲染器进程中使用Javascript 电子5.0.0“;未捕获引用错误:未定义require“;,javascript,node.js,electron,require,Javascript,Node.js,Electron,Require,我最初使用的是electron stable(4.x.x),能够在浏览器和渲染器进程中使用require。我升级到electron beta(5.0.0),因为我需要更新版本的node,并且在渲染器进程中遇到此错误消息,未捕获引用错误:未定义require 通过谷歌搜索和浏览电子文档,我发现一些评论说错误可能是由于在初始化浏览器窗口时将webPreferences.nodeIntegration设置为false引起的;e、 g.:新建浏览器窗口({宽度、高度、网页首选项:{nodeIntegra
require
。我升级到electron beta(5.0.0),因为我需要更新版本的node,并且在渲染器进程中遇到此错误消息,未捕获引用错误:未定义require
通过谷歌搜索和浏览电子文档,我发现一些评论说错误可能是由于在初始化
浏览器窗口时将webPreferences.nodeIntegration
设置为false引起的;e、 g.:新建浏览器窗口({宽度、高度、网页首选项:{nodeIntegration:false})代码>。但我没有这样做,所以我认为一定是其他问题,并继续寻找解决方案。结果是,节点集成在以前的electron版本中默认为true,但在5.0.0中默认为false。因此,将其设置为真解决了我的问题。在评论或电子页面上找不到在线记录的这一变化,我想我应该让这篇自我回复的文章更容易为未来遇到这一问题的人找到。如前所述,nodeIntegration
现在在5.0.0中默认为false
有一些关于如何设置此值的示例代码
let win = new BrowserWindow({
webPreferences: {
nodeIntegration: true
}
})
win.show()
是正确的,nodeIntegration
在v5.0.0中默认为false
这是的其他更改
部分中的最后一条语句,在创建新浏览器窗口时也在将nodeIntegration设置为true中提到
app.on('ready', () => {
mainWindow = new BrowserWindow({
webPreferences: {
nodeIntegration: true
}
});
});
这篇文章的读者在做决定之前应该先阅读文章中的部分
// Bad
const mainWindow = new BrowserWindow({
webPreferences: {
nodeIntegration: true,
nodeIntegrationInWorker: true
}
})
mainWindow.loadURL('https://example.com')
// Good
const mainWindow = new BrowserWindow({
webPreferences: {
preload: path.join(app.getAppPath(), 'preload.js')
}
})
mainWindow.loadURL('https://example.com')
电子版本12及以上
const electron = require("electron");
const { app, BrowserWindow } = electron;
app.on("ready", () => {
const mainWindow = new BrowserWindow({
width: 1000,
height: 600,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: true,
},
});
mainWindow.loadURL(`file://${__dirname}/index.html`);
});
在webPreferences
中添加contextIsolation:false
设置contextIsolation:false
在main.js中保留以下代码
new BrowserWindow({
width: 800, height: 600,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: true,
}
})
对于电子13.0.0
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: true
}
虽然当我在我的electron应用程序(electron v4.1.4)上打开DevTool时,它会显示一条警告,在主要版本中,默认情况下nodeIntegeration将变为false。这对我很有帮助!!来自未来银行的问候你,它真的帮了我的忙,我对ElectronHow转换const electron=require('electron')感到不安
this?像这样preload:path.join(app.getAppPath(),'electron.js')
?electron.js不是一个文件。实际位置为node\u modules\electron\index.js
。如何将其作为预加载?您只能预加载direct js文件,不能预加载节点模块。谢谢!我正在寻找一种替代方法,而不仅仅是启用节点集成
。参考webPreferences:{nodeIntegration:true,contextIsolation:false,enableMoteModule:true,},这三行代码救了我的命,谢谢,contextIsolation:false
为我修复了它,它只在旧版本中有效,没有那种荣誉感,这非常有效,谢谢,这对我有效。我缺少enableRemoteModule。当我将标志设置为true,甚至尝试将const fs=require('fs')
放置在renderer.js文件以及我使用的js脚本文件中时,你知道为什么这对我仍然不起作用吗?有同样的问题,从对我起作用的3天起,它就起作用了。谢谢