【问题标题】:InternalError Metro has encountered an error: While trying to resolve module `child_process`InternalError Metro 遇到错误:尝试解析模块“child_process”时
【发布时间】:2021-03-10 13:53:59
【问题描述】:

我正在尝试从 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:

我该如何解决这个问题?

【问题讨论】:

    标签: javascript reactjs npm expo child-process


    【解决方案1】:

    由于某些原因它不起作用

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

    只有将代码重写为 javascript 才有意义的解决方案。

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

    【讨论】:

    • 嗨,我已经想到了一个替代解决方案,通过在服务器上托管我的 python 代码并对其进行调用,我已经发布了另一个问题,如果你可以看看吗?谢谢stackoverflow.com/questions/66597974/…
    猜你喜欢
    • 2022-10-21
    • 2023-02-25
    • 2019-06-24
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-05
    • 1970-01-01
    相关资源
    最近更新 更多