【问题标题】:TypeError: cb.apply is not a function类型错误:cb.apply 不是函数
【发布时间】:2020-08-02 10:37:55
【问题描述】:

我正在尝试运行使用 Metro Bundler 的现有 React Native 项目。我的操作系统是 Ubuntu 20.04,我尝试在物理 Android 设备和使用 Android Studio 创建的 Android 模拟器上运行该应用程序。

我已经成功地克隆了项目的 repo,安装了它的依赖项 (yarn),构建了应用程序 (react-native run-android),并在我的手机和模拟器上打开了应用程序。但是,运行react-native start 会出错,说TypeError: cb.apply is not a function。以下是我在运行react-native start 时得到的完整信息和错误:

$ react-native start
warn Your project is using deprecated "rnpm" config that will stop working from next release. Please use a "react-native.config.js" file to configure the React Native CLI. Migration guide: https://github.com/react-native-community/cli/blob/master/docs/configuration.md
warn The following packages use deprecated "rnpm" config that will stop working from next release:
  - rn-fetch-blob: https://npmjs.com/package/rn-fetch-blob
Please notify their maintainers about it. You can find more details at https://github.com/react-native-community/cli/blob/master/docs/configuration.md#migration-guide.
┌──────────────────────────────────────────────────────────────────────────────┐
│                                                                              │
│  Running Metro Bundler on port 8081.                                         │
│                                                                              │
│  Keep Metro running while developing on any JS projects. Feel free to        │
│  close this tab and run your own Metro instance if you prefer.               │
│                                                                              │
│  https://github.com/facebook/react-native                                    │
│                                                                              │
└──────────────────────────────────────────────────────────────────────────────┘

warn Your project is using deprecated "rnpm" config that will stop working from next release. Please use a "react-native.config.js" file to configure the React Native CLI. Migration guide: https://github.com/react-native-community/cli/blob/master/docs/configuration.md
warn The following packages use deprecated "rnpm" config that will stop working from next release:
  - rn-fetch-blob: https://npmjs.com/package/rn-fetch-blob
Please notify their maintainers about it. You can find more details at https://github.com/react-native-community/cli/blob/master/docs/configuration.md#migration-guide.
Looking for JS files in
   /home/ggiuffre/Documents/squib/app 

Loading dependency graph, done.
 BUNDLE  [android, dev] ./index.js ░░░░░░░░░░░░░░░░ 0.0% (0/1)/home/ggiuffre/Documents/squib/app/node_modules/@react-native-community/cli/node_modules/graceful-fs/polyfills.js:285
        if (cb) cb.apply(this, arguments)
                   ^

TypeError: cb.apply is not a function
    at /home/ggiuffre/Documents/squib/app/node_modules/@react-native-community/cli/node_modules/graceful-fs/polyfills.js:285:20
    at FSReqCallback.oncomplete (fs.js:169:5)

安装graceful-fs(由another post 推荐关于同样的问题)并没有改变任何东西,我仍然得到同样的错误。

这可能是什么问题?提前致谢。

【问题讨论】:

    标签: android react-native metro-bundler


    【解决方案1】:

    我在 CI 上遇到了非常相似的问题,但它在我的本地机器(节点 13)上正常工作。在 CI 中,当使用我两个月前发布的代码在 CircleCI 或 AppCenter 上构建项目时,它会引发以下错误。它只是没有意义,就像节点已经动态损坏了。

    我用节点 10、12 和 14 测试了相同的代码,但现在它只适用于节点 10 (10.22.0)。

    我遇到的错误:

    /home/circleci/my-app/node_modules/@react-native-community/cli/node_modules/graceful-fs/polyfills.js:285
            if (cb) cb.apply(this, arguments)
                       ^
    
    TypeError: cb.apply is not a function
        at /home/circleci/my-app/node_modules/@react-native-community/cli/node_modules/graceful-fs/polyfills.js:285:20
        at FSReqCallback.oncomplete (fs.js:169:5)
    
    > Task :app:bundleReleaseJsAndAssets FAILED
    
    FAILURE: Build failed with an exception.
    
    * What went wrong:
    Execution failed for task ':app:bundleReleaseJsAndAssets'.
    > Process 'command 'node'' finished with non-zero exit value 1
    

    更新

    解决我的问题的方法是向package.json 添加一个解决方案,以不允许任何lib 使用“4.2.4”之前的graceful-fs 版本。现在它再次与节点 12 一起工作。

    PS:不要忘记运行yarnnpm run install 来更新您的.lock。如果此解决方案不适合您,请在此 thread 上添加与节点 12.18.3 上的此问题相关的评论

      "devDependencies": {
        ...
      },
      "resolutions": {
        "graceful-fs": "4.2.4"
      },
    

    【讨论】:

    • 天啊,我喜欢这个答案,你的更新救了我。几个小时以来一直在尝试解决这个问题,尝试了网络上所有其他可用的解决方案,但没有任何效果。谢谢!
    • 非常感谢。
    【解决方案2】:

    我今天在构建时也遇到了这个问题。 (运行节点 12.8.3)

    我重新安装了以下软件包:

    npm install graceful-fs --save-dev
    

    这样就解决了上面的问题。

    【讨论】:

      【解决方案3】:

      我在两台电脑上都解决了。需要做更多的工作。

      • 选项 1:

        • 关注此目录C:\Users(your username)\AppData\Roaming
        • 删除 npm 文件夹
        • 如果有一个 npm 缓存 文件夹。在 Windows 上运行 npm clean cache -forcenpm cache clean -force(——现在需要强制清理缓存)
        • 如果不执行选项 2,您现在应该很好。
      • 选项 2:

        • 关注此目录C:\Users(your username)\AppData\Roaming
        • 删除 npm 文件夹
        • 如果有一个 npm 缓存 文件夹。在 Linux 上运行 npm clean cache -force,或在 Windows 上运行 npm cache clean(——现在需要强制清理缓存)
        • 确保删除所有与 Nodejs 相关的内容
        • 并卸载了 NodeJS。
        • 重新安装 Nodejs。
        • 你现在应该很好了。

      【讨论】:

        【解决方案4】:

        这里有一个 Ubuntu 20.04 用户,节点为 16.0.0。我在运行时遇到了同样的错误:

        > npx create-react-app [my app]
        

        我发现我必须重新安装 create-react-app (先从 node_modules 中删除它)才能解决它。

        > npm install create-react-app
        

        【讨论】:

        • 啊,是的,谢谢你分享这个。我没有安装它,而不是抱怨该模块不存在,而是显示问题中的错误。也许你应该拥有它 --save-dev 因为它不是生产所需的依赖项?
        【解决方案5】:

        如果你安装了 nvm,那就更简单了。要检查这一点,请运行

        nvm --version
        

        然后安装最新的lts版本的node,运行

        nvm install --lts
        

        这对我有用,如果有帮助请告诉我

        【讨论】:

          【解决方案6】:

          如果您使用的是 Windows 10,我通过执行以下操作解决了这个问题:

          首先,这是错误:

          1. 导航到%USERPROFILE%\AppData\Roaming\
          2. 通过键入Run npm clean cache —force 删除可能存在的任何缓存
          3. 删除NPM 文件夹
          4. 重新安装 Node.js 并确保在安装 Node.js 期间包含 NPM

          完成上述步骤后,我可以像以前一样运行 npm 并添加我需要的模块。

          【讨论】:

            【解决方案7】:

            在我的情况下,我尝试使用 Husky 并出现此错误:

            husky > commit-msg (node v15.6.0)
            npm ERR! cb.apply is not a function
            
            npm ERR! A complete log of this run can be found in:
            npm ERR!     ~/.npm/_logs/2021-01-24T06_04_04_218Z-debug.log
            
            >>> Installation [ 'commitlint@latest' ] fail with code 1 <<<
            
            husky > commit-msg hook failed (add --no-verify to bypass)
            

            我运行下面的命令,问题就解决了

            npm i -D commitlint
            

            【讨论】:

              【解决方案8】:

              在 Windows 上:

              1. 转到路径 C:/Users/{User}/AppData/Roaming

              2. 删除 2 个文件夹 npm npm 缓存 在路径

              3. npm 清理缓存——强制

              【讨论】:

              • 第三步:npm cache clean --force
              【解决方案9】:

              您的节点版本与您尝试使用的库/框架不匹配。 尝试更新您的节点版本并检查。

              最好使用 nodist/nvm 来做升级或降级。

              【讨论】:

                猜你喜欢
                • 2020-12-05
                • 2022-07-02
                • 1970-01-01
                • 2016-02-26
                • 2013-04-01
                • 2016-03-06
                相关资源
                最近更新 更多