1

I am currently on the backend of my software which runs via electron but I can't get the data. So I tried to retrieve the data from a python program that sends it using Flask, but when I run the npm start command it gives me the following error message:

Module not found: Error: Can't resolve 'child_process' in 'C:\Users\cantr\Desktop\Stage ingénieur KOMILFO SPORT\Sail Vision\React\react-electron\src\pages'

Here is my Javascript and python script:

JS:

import React from "react";

class Home extends React.Component {
    constructor() {
        super();
        this.hello = require("child_process").spawn("python", ["./hello.py"]);
        this.state = { fs_s5: 12 };
        this.hello.stdout.on('data', (data) => {
          this.setState({ fs_s5: data });
        });
    }
    render(){
        return(
        <div>
            <div class="home">
                <div class="template-1" id="temp1">
                <div class="panel-1">
                    <div class="panel-header">
                    <h1>Panel 1</h1>
                    <i class='bx bx-cog modal-trigger-panel'></i>
                    </div>
                    <div class="panel-body">
                    <div class="sec-5 modal-trigger-data" id="hs-sec-5">
                        <span class="h1" id="h1-fs-s5">{this.state.fs_s5}</span>
                        <h2>TWIST</h2>
                        <h3>s5</h3>
                    </div>
                    <div class="sec-4 modal-trigger-data" id="hs-sec-4">
                        <h1>--</h1>
                        <h2>TWIST</h2>
                        <h3>s4</h3>
                    </div>
                    <div class="sec-3 modal-trigger-data" id="hs-sec-3">
                        <h1>--</h1>
                        <h2>TWIST</h2>
                        <h3>s3</h3>
                    </div>
                    <div class="sec-2 modal-trigger-data" id="hs-sec-2">
                        <h1>--</h1>
                        <h2>TWIST</h2>
                        <h3>s2</h3>
                    </div>
                    <div class="sec-1 modal-trigger-data" id="hs-sec-1">
                        <h1>--</h1>
                        <h2>TWIST</h2>
                        <h3>s1</h3>
                    </div>
                    </div>
                </div>
                </div>
            </div>
        </div>    
        );
    }
}

export default Home;

Python:

from __future__ import print_function
import time
from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World! This is powered by Python backend."

if __name__ == "__main__":
   print(12)
   time.sleep(5)
   app.run(host='127.0.0.1', port=5000)

Regards,

1 Answer 1

2

Try to add the following in your package.json file:

{
  ...
  // add this to package.json 
  "browser": {
    "child_process": false
  }
}
3
  • Thanks I don't get the error at startup but after I run the npm start command there is nothing displayed when the page appears. It may be due to my package.json.
    – Feyto
    Commented Jan 3, 2023 at 7:58
  • what makes changing the child_process value to 'false' work?
    – Feyto
    Commented Jan 3, 2023 at 8:18
  • 1
    My understanding is that webpack tries to bundle the child_process module for the client code, running in the browser. But the child_process is only available in Node.js. So setting it to false tells webpack that it's ok to use an empty module. Commented Jan 3, 2023 at 18:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.