react-native run-ios JSON Error

Posted on
react-native xcode cli

Today I ran into a weird issue while trying out the react-native-cli

➜ npx react-native run-ios
info Found Xcode workspace "testApp.xcworkspace"
info Launching iPhone 11 (iOS 13.6)
info Building (using "xcodebuild -workspace testApp.xcworkspace -configuration Debug -scheme testApp -destination id=8D7B745B-A853-4B61-BC1C-9B4A8C6ED956")

error Unexpected end of JSON input. Run CLI with --verbose flag for more details.
SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at getTargetBuildDir (/full/path/testApp/node_modules/@react-native-community/cli-platform-ios/build/commands/runIOS/index.js:302:25)
    at getBuildPath (/full/path/testApp/node_modules/@react-native-community/cli-platform-ios/build/commands/runIOS/index.js:330:26)
    at runOnSimulator (/full/path/testApp/node_modules/@react-native-community/cli-platform-ios/build/commands/runIOS/index.js:183:19)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async Command.handleAction (/full/path/testApp/node_modules/@react-native-community/cli/build/index.js:186:9)

The --verbose flag was not helpful

I tried to google the error but quickly realised that most of the threads on SO were not related. After a few hours trying to find more information on the specific error, uninstalling and reinstalling/upgrading node, npm, npx, react-native, react-native-cli, commandline-tools and xcode multiple times, I was about to create a new bug on the react-native GitHub page.

The bug template included a step to paste the output of the react-native info command. Here was mine before I managed to fix the issue.

➜ react-native info
System:
    OS: macOS 10.15.6
    CPU: (8) x64 Intel(R) Core(TM) i7-8569U CPU @ 2.80GHz
    Memory: 72.41 MB / 16.00 GB
    Shell: 5.7.1 - /bin/zsh
  Binaries:
    Node: 14.5.0 - /usr/local/bin/node
    Yarn: 1.22.4 - /usr/local/bin/yarn
    npm: 6.14.5 - /usr/local/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  Managers:
    CocoaPods: 1.9.3 - /usr/local/bin/pod
  SDKs:
    iOS SDK: Not Found
    Android SDK: Not Found
  IDEs:
    Android Studio: Not Found
    Xcode: /undefined - /usr/local/bin/xcodebuild
  Languages:
    Java: Not Found
    Python: 2.7.16 - /usr/bin/python
  npmPackages:
    @react-native-community/cli: Not Found
    react: 16.13.1 => 16.13.1
    react-native: 0.63.1 => 0.63.1
  npmGlobalPackages:
    *react-native*: Not Found

Notice the Xcode: /undefined - /usr/local/bin/xcodebuild? I had tried to manually build the workspace from the command line using the xcodebuild -workspace testApp.xcworkspace ... command logged as info while running react-native run-ios while debugging the error earlier, but there was no error, or output really, when running it. Because I never used the xcodebuild command I thought nothing of it.

It turns out that not having any output from running xcodebuild was the main clue as to why the JSON expected by the run-ios script was empty, and why the react-native info returned an undefined Xcode version.

After that it was just a matter of checking the link origin for /usr/local/bin/xcodebuild/usr/bin/true 🤦‍♂️

Making sure that /usr/bin/xcodebuild -workspace testApp.xcworkspace ... was actually building doing something, I deleted the /usr/local/bin/xcodebuild link and recreated it to point to /usr/bin/xcodebuild instead and voila!

➜ react-native info
info Fetching system and libraries information...
...
  SDKs:
    iOS SDK:
      Platforms: iOS 13.6, DriverKit 19.0, macOS 10.15, tvOS 13.4, watchOS 6.2
    Android SDK: Not Found
  IDEs:
    Android Studio: Not Found
    Xcode: 11.6/11E708 - /usr/local/bin/xcodebuild
...

Conclusion

Always check what you are pasting into your terminal before creating a fake link between scripts 😇

Back home