[英]Can't resolve 'child_process'
I am currently on the backend of my software which runs via electron but I can't get the data.我目前在通过 electron 运行的软件后端,但无法获取数据。 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:因此,我尝试从使用 Flask 发送数据的 python 程序中检索数据,但是当我运行 npm 启动命令时,它给了我以下错误消息:
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:这是我的 Javascript 和 python 脚本:
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: 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,问候,
Try to add the following in your package.json
file:尝试在您的package.json
文件中添加以下内容:
{
...
// add this to package.json
"browser": {
"child_process": false
}
}
The error message you're receiving is because the child_process module is not a built-in module in Node.js, and it is not part of the dependencies of your project.您收到的错误消息是因为 child_process 模块不是 Node.js 中的内置模块,它不是项目依赖项的一部分。 child_process is a module in Node.js that allows you to spawn new processes, execute external commands, and manage child processes. child_process 是 Node.js 中的一个模块,它允许您生成新进程、执行外部命令和管理子进程。
To fix this issue, you need to install the child_process module by running the following command in your project's root directory:要解决此问题,您需要通过在项目的根目录中运行以下命令来安装 child_process 模块:
npm install child_process
It should work after installing the child_process module.它应该在安装 child_process 模块后工作。 Also, the python script you've shared only runs a flask server on localhost, port 5000. But you're trying to get data from it via child_process.spawn, which is trying to run the python script as a child process.此外,您共享的 python 脚本仅在本地主机端口 5000 上运行 flask 服务器。但是您正试图通过 child_process.spawn 从中获取数据,它试图将 python 脚本作为子进程运行。 However, the python script is not returning any data to the child process, it's just running a web server and waiting for requests.但是,python 脚本没有向子进程返回任何数据,它只是运行一个 web 服务器并等待请求。 You should modify your python script to return the data you want to display in your frontend.您应该修改 python 脚本以返回要在前端显示的数据。
You can also consider using http request library like Axios or Fetch to get the data from python flask server.也可以考虑使用 http 请求库如Axios 或 Fetch从 python flask 服务器获取数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.