【问题标题】:Integrate react-native version 0.62.2 into existing IOs app error将 react-native 版本 0.62.2 集成到现有 IO 应用程序错误
【发布时间】:2020-08-14 22:45:31
【问题描述】:

按照 react-native 文档将 react-native 集成到现有的 IO 应用程序中。目录结构作为文档集成是

android/
ios/
   Podfile
   #...other ios file
node_modules/
  react-native
  react
  @react-native-community  
package.json
.... 

这样当从 Podfile 运行 pod install 时可以调用函数 use_native_modules

require_relative './libs/react-native-libs/node_modules/@react-native-community/cli-platform-ios/native_modules'
...
use_native_modules!

但由于不改变我现有的项目结构,我想组织不同的项目,

所以我添加了 react-native-integrate 模块,就像我的 IOs 项目的一部分一样

my_ios_project
  Podfile
  libs/
     ... other modules
     react-native-integrate
         node_modules
             react-native
             @react-native-community
             react 
         package.json

然后我改变相对模块的路径:

require_relative './libs/react-native-libs/node_modules/@react-native-community/cli-platform-ios/native_modules'
...
use_native_module!   

但问题在于native_modules 文件和node_modules 中的其他库使用绝对require。例如在@react-community/cli-platform-ios/native_module.rb 中有这样一行

cli_resolve_script = "try {console.log(require('@react-native-community/cli').bin);} catch (e) {console.log(require('@react-native/cli'))}"

需要绝对路径'@react-native-community'。因此,当我从父目录运行 Pod install 我的 pod 文件时,node 无法识别我的子模块,从而导致错误。

据我所知, node_module 的路径列表基本上是从当前目录到根目录的每个目录下的 node_modules 目录列表。而当前目录就是调用 require 'native_module.rb' 的 Podfile 的目录 你能给我一个解决这个问题的解决方案吗,比如从子目录添加节点路径或其他什么?谢谢。

【问题讨论】:

    标签: ios react-native nodes node-modules


    【解决方案1】:

    我找到了解决办法:

    • 在 ios 根目录下添加 package.json 文件,属性为main
    • 在根 ios 项目中添加 react-native.config.js,编辑cli-platform-ioscli-platform-android 的路径:
    'use strict';
    
    const ios = require('./node_modules/@react-native-community/cli-platform-ios');
    const android = require('./node_modules/@react-native-community/cli-platform-android');
    
    module.exports = {
      commands: [...ios.commands, ...android.commands],
      platforms: {
        ios: {
          linkConfig: ios.linkConfig,
          projectConfig: ios.projectConfig,
          dependencyConfig: ios.dependencyConfig,
        },
        android: {
          linkConfig: android.linkConfig,
          projectConfig: android.projectConfig,
          dependencyConfig: android.dependencyConfig,
        },
      }
    };
    
    • 再次运行pod install

    在这里查看我的演示: https://github.com/lehoangnam97/demo-intergrate-rn-on-ios

    【讨论】:

      猜你喜欢
      • 2018-08-29
      • 2017-05-02
      • 1970-01-01
      • 2023-03-09
      • 2016-01-01
      • 2017-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多