2

I started to create an electron application with reactjs. I'm using react-react-app method to create react application. In my application I want to execute some cli commands. Every time when I use 'child_process' I'm getting errors. sometimes it's says 'child_process.exec isn't a function'. so how can I execute/use 'child_process' methods/functions inside this project. Thank you so much

cli.js

export const cli = () => {
 try {
  const { spawn } = require('child_process');
  const ls = spawn('ls', ['-lh', '/usr']);

  ls.stdout.on('data', (data) => {
    console.log(`stdout: ${data}`);
  });

  ls.stderr.on('data', (data) => {
    console.error(`stderr: ${data}`);
  });

  ls.on('close', (code) => {
    console.log(`child process exited with code ${code}`);
  });
 } catch(error) {
  console.log(error)
 }

}

app.js

import { cli } from "./cli";

function App() {

// call cli function
cli();

return (
<div className="App">
  <header className="App-header">
    <img src={logo} className="App-logo" alt="logo" />
    <p>
      Edit <code>src/App.js</code> and save to reload.
    </p>
    <a
      className="App-link"
      href="https://reactjs.org"
      target="_blank"
      rel="noopener noreferrer"
    >
      Learn React
    </a>
  </header>
</div>
);

}

dkz
  • 39
  • 3
  • 1
    can u add more informations, errors to you problem – namila007 Apr 26 '20 at 22:20
  • Enable your `nodeintegration` flag when you are creating your `browserWindow`. – tpikachu Apr 27 '20 at 01:22
  • https://stackoverflow.com/questions/60200745/require-is-not-defined-error-comes-when-i-try-to-import-js-file-with-node-mod/60201916#60201916 – tpikachu Apr 27 '20 at 01:23
  • And I'd recommend you to run your child process at your main process, not in the renderer. – tpikachu Apr 27 '20 at 01:23
  • @tpikachu wow, you have given number of answers for my number of questions. Thanks – dkz Apr 27 '20 at 20:35
  • @tpikachu how can I use functions inside react files, which I have written in the main file? after enabling `nodeintegration` can I do `module.exports` and `import` it? that part is bit unclear – dkz Apr 29 '20 at 04:31
  • `module.exports` is commonJS syntax and `import` is from ES6 or Higher. – tpikachu Apr 29 '20 at 09:15
  • Not related to `nodeintegration`. `import` or `module.exports` is just related which ES version you are using at your project – tpikachu Apr 29 '20 at 09:16
  • You can transpile this ES6 or higher to commonJS by using `babel` – tpikachu Apr 29 '20 at 09:20
  • https://medium.com/@arunrajeevan/import-export-vs-require-module-exports-572f63516745 – tpikachu Apr 29 '20 at 09:20
  • Sorry for my stupid question.. Actually what I want to do is create a file inside react application and write `child_process` function there ( like a common function ) and use it some where else. I already tried that but I', getting `spaw is not a function` error. I update my question with example please check it. Thank you – dkz Apr 29 '20 at 18:42

0 Answers0