如何在 ReactJS 应用程序的服务器端导入模块 'child_process'

how to import module 'child_process' on server side of a ReactJS app

如何在 ReactJS 应用程序中使用像 'child_process' 这样的模块?
(react@16.3.1 / Linux)

这些语法失败:

import { spawn } from 'child_process'
const ls = spawn('ls', ['-al']);

产量:TypeError: Object(...) is not a function

import cp from 'child_process';
const ls = cp.spawn('ls', ['-al']);

产量:TypeError: __WEBPACK_IMPORTED_MODULE_3_child_process___default.a.spawn is not a function

var { spawn } = require('child_process');
const ls = spawn('ls', ['-al']);  

产量:TypeError: spawn is not a function

ReactJS 仅在您应用程序的客户端工作并与 DOM 交互。子进程在应用程序的服务器端触发。

所以这两件事是分开的。

ReactJS 可以对服务器进行 http 调用,然后触发子进程。服务器可以将子进程的结果发送回 React 客户端。

您不能在 React 应用程序中使用节点的模块 child_process

您无法访问 OS 系统进程以从浏览器生成和分叉,因为它是一个孤立的环境。