【问题标题】:UnhandledPromiseRejectionWarning: Error: Cannot find module '/MyProject/node_modules/react-native-scripts/build/scripts/init.js'UnhandledPromiseRejectionWarning:错误:找不到模块'/MyProject/node_modules/react-native-scripts/build/scripts/init.js'
【发布时间】:2019-02-27 14:01:15
【问题描述】:

我正在尝试使用 Facebook React Native 教程构建一个新的 React Native 项目。该命令以前对我有用,但突然间我收到此错误消息。

(node:14866) UnhandledPromiseRejectionWarning: Error: Cannot find module '/MyProject/node_modules/react-native-scripts/build/scripts/init.js'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
at Function.Module._load (internal/modules/cjs/loader.js:507:25)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:20:18)
at _callee2$ (/.config/yarn/global/node_modules/create-react-native-app/build/index.js:128:32)
at tryCatch (/.config/yarn/global/node_modules/regenerator-runtime/runtime.js:62:40)
at Generator.invoke [as _invoke] (/.config/yarn/global/node_modules/regenerator-runtime/runtime.js:296:22)
at Generator.prototype.(anonymous function) [as next] (/.config/yarn/global/node_modules/regenerator-runtime/runtime.js:114:21)
at step (/.config/yarn/global/node_modules/babel-runtime/helpers/asyncToGenerator.js:17:30)
at /.config/yarn/global/node_modules/babel-runtime/helpers/asyncToGenerator.js:28:13
(node:14866) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:14866) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

我不确定发生了什么变化,但我找不到解决方法。有人可以帮忙吗?

编辑:在这里找到启动 React Native 应用程序的新方法:https://github.com/react-community/create-react-native-app

答案如下。

【问题讨论】:

    标签: node.js reactjs react-native


    【解决方案1】:

    你可能正在看一个旧的教程,最近有一些变化与 react-native https://github.com/react-community/create-react-native-app/issues/770

    【讨论】:

    【解决方案2】:

    Create React Native App 已更改,现在使用新的 Expo CLI。

    我按照这里的说明进行操作:https://github.com/react-community/create-react-native-app

    $ npm install -g expo-cli
    $ expo init AwesomeProject
    $ cd AwesomeProject
    $ expo start
    

    它现在将打开一个新的 localhost Metro Bundler,它有一个 GUI,允许您运行模拟器、发送链接或发布您的项目。

    【讨论】:

    • 我也这样做了。工作正常。但在弹出代码后,给我错误“捆绑失败:NotFoundError:在任何根目录中找不到入口文件index.js”
    • 他们每次都在改变一些东西。这是不好的。所有文档都在网络中使用 create-react-native-app。我讨厌javascript世界每次都在变化,所有文档都过时了。
    • @KedarDave 我在这里发布了一个解决方案:github.com/wix/react-native-navigation/issues/…
    • React-Native 的变化太大了,就像又学习了 react-native。不知道他们为什么把简单的东西弄得这么复杂。
    【解决方案3】:

    这些 javascript 库正在迅速变化。如果您使用的是 yarn,这在 2020 年对我来说适用于 macOS

    brew install yarn
    sudo yarn global add expo-cli
    expo init <project name>
    

    然后会创建一个名为

    的项目
    cd <project name>
    yarn start
    

    【讨论】:

      【解决方案4】:

      发生错误,因为 create-react-native-app 使用了错误版本的 react-native-scripts。它使用 react-native-scripts 的最新稳定版本,即 2.01。此版本不包含错误消息中提到的 init.js 文件。

      要修复错误,我们需要使用与 create-react-native-app 兼容的 react-native-scripts 版本。 react-native-scripts 1.4.0 版似乎兼容,因为它是在 create-react-native-app 几个月后发布的。

      所以要使用 create-react-native-app 创建一个新项目,请使用命令:create-react-native-app --scripts-version 1.4.0 [project-name]。此解决方案适用于 create-react-native-app 版本 1.0.0

      【讨论】:

        猜你喜欢
        • 2021-04-02
        • 2019-12-30
        • 2018-12-05
        • 1970-01-01
        • 2020-07-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多