[英]Child process spawn is producing TypeError “stdout.on is not a function” in Electron
正如标题所说,我在尝试为 spawn.stdin.on Uncaught TypeError: spn.stdout.on is not a function
创建事件处理程序时遇到错误。 我正在使用 contextBridge ,如下所示,每当我创建一个新的 spawn object 时,它就会被执行,但是当我创建事件处理程序时它会抛出一个错误。
preload.js
const {contextBridge, remote} = require('electron');
const spawn = require('child_process').spawn;
contextBridge.exposeInMainWorld(
'api', {
spawn: (cmd, args) => {
return spawn(cmd, args);
}
}
);
以及引发错误的 function。
let spn = undefined;
let running = false;
let finishQueueItem = false;
function startQueue() {
// if the queue is already running, then return
if (running) return;
if (!$q.length > 0) {
let x = addToQueue(getConfig());
if (!x) return;
}
running = true;
let $i = $q.pop();
let cmd = $p.DAINPath + "\\DAINAPP.exe";
let args = parseConfig($i);
spn = api.spawn(cmd, args);
spn.stdout.on("data", (data) => {
console.log(data.toString());
});
spn.stderr.on("data", (data) => {
console.error(data.toString());
});
spn.on("exit", (code) => {
console.log("Child process exited with code " + code.toString());
spn = undefined;
running = false;
});
}
let mainWindow = new BrowserWindow(
{
width: 800,
height: 600,
webPreferences:{
nodeIntegration:true
}
});
请在创建浏览器 window 时添加 nodeIntegration。 您在渲染器中使用节点 API。 当您不启用nodeIntegration
时,您将无法在渲染器中使用任何节点模块。
与此ipcRenderer 未从主进程接收消息类似的情况
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.