如题,运行环境为
Windows 10 x64 20H2,
Electron v11.1.1,
Chromium v87.0.4280.88,
Node v12.18.3
项目主进程main.js如下:
const { app, BrowserWindow, Menu } = require('electron')
const { chmod } = require('fs')
const { join } = require('path')
const { pathToFileURL } = require('url')
const { runInContext } = require('vm')
function createWindow () {
const win = new BrowserWindow({
width: 980,
height: 580,
//frame: false,
resizable: false,
webPreferences: {
nodeIntegration: true,
preload: join(__dirname,'renderer.js')
}
})
win.loadFile('interface/index.html')
}
app.whenReady().then(createWindow)
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
})
//Menu.setApplicationMenu(null)
渲染进程renderer.js如下:
window.electron=require('electron');
const exec = require('child_process').exec;
const fs=require('fs');
const {ipcRenderer} = require('electron').ipcRenderer;
const { windowsStore } = require('process');
const { Script } = require('vm');
const { electron } = window.electron;
function RunKit()//运行项目根目录的main.exe
{
var cmd = "main.exe";
exec(cmd, function(error, stdout, stderr) { console.log(stdout); });
}
function Order(Args)
{
fs.writeFile('file.in',Args);
}
命令行输入npm start运行时控制台出现如下报错:
Uncaught ReferenceError: require is not defined at renderer.js:2
Uncaught ReferenceError: Cannot access 'exec' before initialization at RunKit (renderer.js:13)
后一个报错估计是前一个报错导致解释中止导致的,那么前一个报错究竟是为什么会产生?我对照着百度上说的检查了main.js中的nodeIntegration: true
是没问题的。已经在这里卡了两天了,求大佬解救。
我今天构建Electron 12的时候也卡在这了,nodeIntegration,preload都试了不行。
以前用Electron还没遇到过。。。
查了下stackoverflow,新版本默认contextIsolation: true导致的,下面增加一条contextIsolation: false。
https://stackoverflow.com/que...