简体   繁体   English

React 应用程序中的 require('child_process').fork 方法“不存在”

[英]require('child_process').fork method “does not exist” in React app

I want to run another nodejs script from a React app I'm writing with node.我想从我用 node 编写的 React 应用程序运行另一个 nodejs 脚本。 I've already used before this post a method I found on this forum, to achieve a similar thing, and it worked just fine: https://stackoverflow.com/a/22649812/11340913 .在这篇文章之前,我已经使用了我在这个论坛上找到的一种方法,以实现类似的效果,并且效果很好: https://stackoverflow.com/a/22649812/11340913 This is my App.js fragment:这是我的 App.js 片段:

import React from 'react';
import Peer from 'peerjs';

const childProcess = require('child_process');

function runScript(scriptPath, callback) {

    // keep track of whether callback has been invoked to prevent multiple invocations
    let invoked = false;

    let process = childProcess.fork(scriptPath);

    // listen for errors as they may prevent the exit event from firing
    process.on('error', err => {
        if (invoked) return;
        invoked = true;
        callback(err);
    });

    // execute the callback once the process has finished running
    process.on('exit', code => {
        if (invoked) return;
        invoked = true;
        var err = code === 0 ? null : new Error('exit code ' + code);
        callback(err);
    });

}

// Now we can run a script and invoke a callback when complete, e.g.
runScript('./node_modules/peer/bin/peerjs', err => {
    if (err) throw err;
    console.log('started p2p server!');
});

const peer = new Peer('banana_nebuna123' ,{
  key: 'peerjs',
  port: 9000,
  host: '127.0.0.1',
  secure: false,
  //path: '/home/eugen/Documents/scripts/ReactProjects/ShareLife/node_modules/peer/bin/peerjs'
}); 

function App() {
  
  return (
    <>
      <h1>
        hello, world!
      </h1>
    </>
  );
}

export default App;

This is the error I'm getting:这是我得到的错误:

TypeError: childProcess.fork is not a function类型错误:childProcess.fork 不是 function

runScript 运行脚本
src/App.js:11 src/App.js:11

 8 | // keep track of whether callback has been invoked to prevent multiple invocations 9 | let invoked = false; 10 | > 11 | let process = childProcess.fork(scriptPath); | ^ 12 | 13 | // listen for errors as they may prevent the exit event from firing 14 | process.on('error', err => {

Node's child_process module, like Node itself, runs in the backend (on the server) and so cannot be run in the browser which lacks access to the operating system in which child processes run. Node 的child_process模块与 Node 本身一样,在后端(在服务器上)运行,因此无法在无法访问运行子进程的操作系统的浏览器中运行。 You might look into web workers which provide similar functionality within the browser.您可能会查看在浏览器中提供类似功能的web workers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 来自child_process和主要master的Node.js分支 - Nodejs fork from child_process and main master 反应原生中的未知模块“child_process” - unknown module “child_process” in react native 为什么我在nodejs中child_process的fork方法的相同代码的不同设备中得到不同的输出? - why I'm getting different outputs in different devices of the same code of the child_process's fork method in nodejs? 节点 child_process 尝试使用 /usr/bin/zsh 并失败,因为它不存在 - Node child_process trying to use /usr/bin/zsh and failing as it does not exist 节点require(&#39;child_process&#39;)。spawn抛出ENOENT - node require('child_process').spawn throws ENOENT 分叉的child_process可以成为父进程,也可以派生新进程吗? - Can a child_process which was forked be a parent and fork new processes as well? 如何在ReactJS应用的服务器端导入模块&#39;child_process&#39; - how to import module 'child_process' on server side of a ReactJS app 在电子应用程序中处理NodeJS的child_process - Handling NodeJS's child_process in electron app 我应该 require('child_process').spawn('node', ['-e', code]) 运行冗长的操作吗? - Should I do require('child_process').spawn('node', ['-e', code]) to run a lengthy operation? child_process.fork-process.send不返回消息 - child_process.fork - process.send does not return message
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM