makovkastar
makovkastar

Reputation: 5020

React Native iOS: Could not build module 'yoga': 'algorithm' file not found

I'm trying to integrate React Native into an existing Swift iOS application using this integration guide: https://facebook.github.io/react-native/docs/integration-with-existing-apps.html. The version of React Native is 0.53.0.

I have successfully installed all the required pods and now trying to build the project, but always getting the following compile error:

enter image description here enter image description here

enter image description here

The error log:

While building module 'yoga' imported from ...node_modules/react-native/React/Base/RCTConvert.h:19:
In file included from <module-includes>:1:
In file included from ...ios/Vandebron/Pods/Target Support Files/yoga/yoga-umbrella.h:15:
In file included from ...node_modules/react-native/ReactCommon/yoga/yoga/YGNode.h:13:
...node_modules/react-native/ReactCommon/yoga/yoga/Yoga-internal.h:11:10: fatal error: 'algorithm' file not found
#include <algorithm>
         ^~~~~~~~~~~
1 error generated.
In file included from ...node_modules/react-native/React/Views/RCTActivityIndicatorViewManager.m:10:
In file included from ...node_modules/react-native/React/Views/RCTActivityIndicatorViewManager.h:10:
In file included from ...node_modules/react-native/React/Views/RCTViewManager.h:13:
...node_modules/react-native/React/Base/RCTConvert.h:19:9: fatal error: could not build module 'yoga'
#import <yoga/Yoga.h

Upvotes: 9

Views: 16031

Answers (3)

Mohammad f
Mohammad f

Reputation: 1061

For react 59 or later you need to do this It also fixes problem of downloading react 11

# Uncomment the next line to define a global platform for your project
platform :ios, '9.0'
target 'root' do
# Uncomment the next line if you're using Swift or would like to use dynamic 
 frameworks
 # use_frameworks!
 # Pods for root
  pod 'React', :path => '../node_modules/react-native'
  pod 'yoga', :path => '../node_modules/react-native/ReactCommon/yoga'
end

post_install do |installer|
 installer.pods_project.targets.each do |target|
  if target.name == "React"
    target.remove_from_project
  end
 end
end

Upvotes: 1

makovkastar
makovkastar

Reputation: 5020

You can put the following snippet in your Podfile to fix this and some other known problems with React Native + Cocoapods. We tested it in our own project and it worked pretty well:

# When using RN in combination with Cocoapods, a lot of
# things are broken. These are the fixes we had to append
# to our Podfile when upgrading to [email protected].
#
# WARNING: Check those line numbers when you're on a different version!

def change_lines_in_file(file_path, &change)
    print "Fixing #{file_path}...\n"

    contents = []

    file = File.open(file_path, 'r')
    file.each_line do | line |
        contents << line
    end
    file.close

    File.open(file_path, 'w') do |f|
        f.puts(change.call(contents))
    end
end


post_install do |installer|

    # https://github.com/facebook/yoga/issues/711#issuecomment-381098373
    change_lines_in_file('./Pods/Target Support Files/yoga/yoga-umbrella.h') do |lines|
        lines.reject do | line |
            [
            '#import "Utils.h"',
            '#import "YGLayout.h"',
            '#import "YGNode.h"',
            '#import "YGNodePrint.h"',
            '#import "YGStyle.h"',
            '#import "Yoga-internal.h"',
            ].include?(line.strip)
        end
    end

    # https://github.com/facebook/yoga/issues/711#issuecomment-374605785
    change_lines_in_file('../../node_modules/react-native/React/Base/Surface/SurfaceHostingView/RCTSurfaceSizeMeasureMode.h') do |lines|
        unless lines[27].include?("#ifdef __cplusplus")
            lines.insert(27, "#ifdef __cplusplus")
            lines.insert(34, "#endif")
        end
        lines
    end

    # https://github.com/facebook/react-native/issues/13198
    change_lines_in_file('../../node_modules/react-native/Libraries/NativeAnimation/RCTNativeAnimatedNodesManager.h') do |lines|
        lines.map { |line| line.include?("#import <RCTAnimation/RCTValueAnimatedNode.h>") ? '#import "RCTValueAnimatedNode.h"' : line }
    end

    # https://github.com/facebook/react-native/issues/16039
    change_lines_in_file('../../node_modules/react-native/Libraries/WebSocket/RCTReconnectingWebSocket.m') do |lines|
        lines.map { |line| line.include?("#import <fishhook/fishhook.h>") ? '#import "fishhook.h"' : line }
    end
end

The source code is taken from this gist: https://gist.github.com/Jpunt/3fe75effd54a702034b75ff697e47578

Upvotes: 2

Thanh Lam
Thanh Lam

Reputation: 696

This is a cocoapods problem. Try to expose only needed headers by edit the yoga.podspec file:node_modules/react-native/ReactCommon/yoga/yoga.podspec add this line at the end of yoga.podspec:

spec.public_header_files = 'yoga/Yoga.h', 'yoga/YGEnums.h',

Look like this:

      source_files = 'yoga/**/*.{cpp,h}'
       source_files = File.join('ReactCommon/yoga', source_files) if ENV['INSTALL_YOGA_WITHOUT_PATH_OPTION']       
       spec.source_files = source_files    spec.source_files = source_files
+
+  # Only expose the needed headers
+  spec.public_header_files = 'yoga/Yoga.h', 'yoga/YGEnums.h', 'yoga/YGMacros.h'
+
end 

Ref: https://github.com/facebook/react-native/issues/17893

The actual pull request: https://github.com/facebook/react-native/pull/17764 . Look at the comment by Plo4ox.

Upvotes: 10

Related Questions