【问题标题】:Child process spawn is producing TypeError "stdout.on is not a function" in Electron子进程 spawn 在 Electron 中产生 TypeError "stdout.on is not a function"
【发布时间】:2020-05-03 04:36:30
【问题描述】:

正如标题所说,我在尝试为 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;
  });
}

【问题讨论】:

  • 你的电子版是?可以分享一下 main.js 吗?
  • Electron 版本为 8.0.1。而且大部分的 JavaScript 都在 main_window.html 文件中。github.com/Gigaboy-01/DAIN-Plus

标签: javascript html node.js electron child-process


【解决方案1】:
let mainWindow = new BrowserWindow(
  {
    width: 800,
    height: 600,
    webPreferences:{
      nodeIntegration:true
    }
  });

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

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

【讨论】:

    猜你喜欢
    • 2017-07-20
    • 2020-04-27
    • 2020-10-06
    • 1970-01-01
    • 1970-01-01
    • 2014-07-26
    • 1970-01-01
    • 1970-01-01
    • 2015-06-24
    相关资源
    最近更新 更多