Search code examples
javascriptreact-nativedebuggingreact-native-cli

How to drop into the debugger for a react-native command?


I'm working on a React Native project in which I get the following error if I try to run react-native run-ios, I get an error Could not find iPhone 6 simulator:

> react-native run-ios
Scanning folders for symlinks in /Users/kurtpeek/mobile/applicant-app/node_modules (16ms)
Found Xcode workspace applicant.xcworkspace

Could not find iPhone 6 simulator

However, this simulator does show up if I run xcrun simctl list --json devices, similar to what is done in node_modules/react-native/local-cli/runIOS/runIOS.js:

> xcrun simctl list --json devices | grep "\"iPhone 6\"" -B 5 -A 5
      },
      {
        "availability" : "(available)",
        "state" : "Shutdown",
        "isAvailable" : true,
        "name" : "iPhone 6",
        "udid" : "93E4D0AF-C267-406B-B8F5-B3B305BEBBF0",
        "availabilityError" : ""
      },
      {
        "availability" : "(available)",

The runOnSimulator function in the aforementioned file reads

function runOnSimulator(xcodeProject, args, scheme) {
  return new Promise((resolve) => {
    try {
      var simulators = JSON.parse(
      child_process.execFileSync('xcrun', ['simctl', 'list', '--json', 'devices'], {encoding: 'utf8'})
      );
    } catch (e) {
      throw new Error('Could not parse the simulator list output');
    }

    const selectedSimulator = findMatchingSimulator(simulators, args.simulator);
    if (!selectedSimulator) {
      throw new Error(`Could not find ${args.simulator} simulator`);
    }

Therefore, I would like to set a debugger breakpoint in node_modules/react-native/local-cli/runIOS/findMatchingSimulator.js. I've tried simply putting debugger statement in it, like so:

function findMatchingSimulator(simulators, simulatorName) {
  debugger;

  if (!simulators.devices) {
    return null;
  }

but this doesn't launch a debugger (like setting an import pdb; pdb.set_trace() in Python, for example). How can I debug react-native CLI commands?


Solution

  • You have 2 options:

    • Upgrade your macOS system and Xcode to the last version
    • Open the simulator application /Applications/Xcode.app/Contents/Developer/Applications/Simulator.app before react-native run-ios

    To launch your debugger statement you need to: