admin管理员组

文章数量:1310532

How does one use a module like 'child_process' from a ReactJS app?
([email protected] / Linux)

These syntaxes fail:

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

yields: TypeError: Object(...) is not a function

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

yields: TypeError: __WEBPACK_IMPORTED_MODULE_3_child_process___default.a.spawn is not a function

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

yields: TypeError: spawn is not a function

How does one use a module like 'child_process' from a ReactJS app?
([email protected] / Linux)

These syntaxes fail:

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

yields: TypeError: Object(...) is not a function

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

yields: TypeError: __WEBPACK_IMPORTED_MODULE_3_child_process___default.a.spawn is not a function

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

yields: TypeError: spawn is not a function

Share Improve this question edited Apr 8, 2018 at 10:02 Francois asked Apr 7, 2018 at 23:00 FrancoisFrancois 2,06622 silver badges40 bronze badges 2
  • What's your use case exactly ? – Bmaed Riasbet Commented Apr 7, 2018 at 23:16
  • Sorry my question was poorly formulated: my app actually has server-side code (where the exception happens) but the error had nothing to do with React. I finally found a way to import module child_process.there. Thanks for your help! – Francois Commented Apr 18, 2018 at 7:26
Add a ment  | 

2 Answers 2

Reset to default 4

You can't use node's module child_process in a react application.

You can't access to OS system processes to spawn and fork from browser because it's an isolated environment.

ReactJS is only working on the client side of you app and interacting with the DOM. Child processes are fired on the server side of your app.

So those two thing are separated.

ReactJS could make an http call for example to the server that will then trigger the child process. The server could send the results of the child process back to the React client.

本文标签: javascripthow to import module 39childprocess39 on server side of a ReactJS appStack Overflow