InternalError Metro has encountered an error: While trying to resolve module `child_process`

InternalError Metro has encountered an error: While trying to resolve module `child_process`

我正在尝试从一个 js 文件调用 python 文件中的一个函数,我通过我的控制台让它工作,但我现在正在尝试使用 expo 在移动应用程序中实现它。

我的设置方式是,我在我的应用程序中有一个特定屏幕的 JS 文件,然后调用一个单独的 JS 文件中的函数,然后调用 python 中的函数文件。

我正在使用 child_process 模块与 JS 中的 python 对话。

正如我所说,在我尝试将 JS 函数导出到我的屏幕文件之前,这是有效的。

index.js



export function foo(process, sentence){

   const spawn = require("child_process").spawn;
   const process = spawn("python3", ["./python.py", sentence]);

   ...
}

screen.js

*other imports

import { foo } from "./filepath..."

...

但是当我 运行 npm start 我得到以下错误:

Failed building JavaScript bundle.
While trying to resolve module `child_process` from file `/Users/mee/Documents/GitHub/project/app/screens/screen.js`, the package `/Users/mee/Documents/GitHub/project/node_modules/child_process/package.json` was successfully found. However, this package itself specifies a `main` module field that could not be resolved (`/Users/me/Documents/GitHub/project/node_modules/child_process/app/screens/screen.js`. Indeed, none of these files exist:

我该如何解决这个问题?

出于某些原因它不会工作

  • child_process 是节点标准库的一部分,它在其他环境如 react-native 或浏览器中不可用
  • 即使上述情况并非如此,您的 phone
  • 上也没有 python3 可执行文件
  • python.py 本地目录中的文件甚至不会上传到 phone,因为捆绑程序只上传一个包含整个 js 代码和资产的大 js 文件,python.py两者都不是。

唯一有意义的解决方案是将该代码重写为 javascript。

从技术上讲这不是不可能的,可能有办法做到这一点,通过为移动平台编译 python 解释器,或者使用一些工具将 python 代码翻译成 js,但这不是什么你应该考虑。