How to debug with breakpoints in React Native
Asked Answered
F

5

19

Please help me how to debug in react native with breakpoints, I found only solution for android real devices, but unable to debug with android emulator and iOS simulator.

Faludi answered 1/4, 2019 at 16:48 Comment(0)
S
22
  1. Install React Native Debugger tool
  • brew update && brew cask install react-native-debugger
  • brew reinstall --cask react-native-debugger
  1. Quit Google Chrome if it already running.
  2. Launch the React Native Debugger app from Applications folder.
  3. Activate iOS simulator and Run command+d using keyboard. In the debug menu select “Debug JS Remotely” option.
  4. Go to React Native Debugger. Select ‘Toggle Developer Tools’ sub menu item from ‘View’ menu.
  5. Select Sources tab (in the React Native Debugger window).
  6. In the left side menu, expand RNDebuggerWorker.js and expand sub option localhost:****. You will see the project root folder.
  7. Select a .js file and add break point. Break point will get activated when the respective line is executed.
  8. You can see the console logs in the console area at the bottom.
Shofar answered 1/4, 2019 at 16:58 Comment(2)
thanks, this helped me find a bug that i was trying to debug for 8+ hoursWalterwalters
Bummer. All I have in that location is the index.bundle?...foo file. No project root, nothing else. :/. And of course, I still can't get the debugger to work. idkBeta
K
5

You can do it by adding debugger; at the line in your code and Chrome will automatically stop at that line.

Kay answered 24/7, 2020 at 12:39 Comment(2)
Thank you. It was so simple.Quilt
I'm thinking this would only work when you are building for webMurtagh
C
1

You can do it by adding breakpoints in your code lines on the source tab of chrome devTools, actually, there is no difference between Google Chrome devTools or React Native Debugger devTools, open the source and find RNDebuggerWorker.js in the left pane and you can find your codes and add breakpoints to debug line by line:

enter image description here

Collaborationist answered 6/7, 2020 at 12:45 Comment(1)
@AmerlicA, try typing the word debugger in your code ;) Otherwise excellent answer!Caston
O
1

You should first enable the debugging on for your Simulator(iOS)/Emulator(Android)

Android: First enable the debugging on by Pressing CMD+M here one popup will appear then click on Debug , it will enable debugging on for that simulator device.

enter image description here

iOS: click CMD+D, popup will appear click on Debug same it will enable debugging for that simulator.

enter image description here

Now run your Applicationn on one of these virtual device(Simulator/Emulator), debugger UI will open in default browser tab(http://localhost:8081/debugger-ui/) now right click and click on Inspect , it will open one window here you will get so many tabs, click on sources and find the File on which you want to debug the code. and here add break point by simply clicking on left side line number like below. here your app will debug and you can see what is happening with the code.

enter image description here

Oid answered 24/3, 2021 at 3:17 Comment(1)
I am using an iOS simulator but on the Safari Under Sources tab it is not showing .js files of my project. What to do here?Enough
S
0

In my case, I had to update my metro config (metro.config.js) to be something like this:

const { getDefaultConfig, mergeConfig } = require('@react-native/metro-config');

/**
 * Metro configuration
 * https://facebook.github.io/metro/docs/configuration
 *
 * @type {import('metro-config').MetroConfig}
 */
const config = {};

module.exports = mergeConfig(getDefaultConfig(__dirname), config);

And then I needed to make sure these were included as part of my package.json:

"@react-native/metro-config": "^0.72.9",
"metro-config": "^0.77.0",

After this was done, i cleaned, and did a new device build (for iOS), and breakpoints started to attach instantly within VS Code after running the debugger (before, I would just get a grey circle).

Sacculate answered 21/7, 2023 at 13:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.