gpt4 book ai didi

javascript - 子进程 spawn 在 Electron 中产生 TypeError "stdout.on is not a function"

转载 作者:行者123 更新时间:2023-12-03 12:40:35 25 4
gpt4 key购买 nike

正如标题所说,我在尝试为 spawn.stdin.on Uncaught TypeError: spn.stdout.on is not a function 创建事件处理程序时遇到错误。 .我正在使用 contextBridge ,如下所示,每当我创建一个新的 spawn 对象时,它就会被执行,但是当我创建事件处理程序时它会抛出一个错误。

preload.js

const {contextBridge, remote} = require('electron');
const spawn = require('child_process').spawn;

contextBridge.exposeInMainWorld(
'api', {
spawn: (cmd, args) => {
return spawn(cmd, args);
}
}
);

以及引发错误的函数。

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
}
});

请在创建浏览器窗口时添加 nodeIntegration。您正在渲染器中使用 Node API。当您不启用 nodeIntegration ,您将无法在渲染器中使用任何 Node 模块。

与此类似的情况
ipcRenderer not receiving message from main process

关于javascript - 子进程 spawn 在 Electron 中产生 TypeError "stdout.on is not a function",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61564717/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com