Loading...

Electron在控制台中报错“xx is not defined”?

当运行项目时,Electron 在控制台中提示 ​​xx is not defined​​​,其中 ​​xx​​ 指代 node 模块。例如:

Uncaught ReferenceError: require is not defined

问题原因

Electron 从 5.0 开始, ​​nodeIntegration​​​ 配置项的默认值由 true 改为了 false ,导致默认情况下不支持 node 模块,比如 ​​require​​​、​​path​​等常用模块。

解决方法

在 Electron 的 ​​main.js​​​ 文件中将 ​​nodeIntegration​​ 配置项改成 true:

let win = new BrowserWindow({
webPreferences: {
nodeIntegration: true, // 请将此项设置为 true
},
});
意见反馈