ChatGPT解决这个技术问题 Extra ChatGPT

How do you debug React Native?

How does one debug their React code with React Native while the app is running in app simulator?

Apart from these answers, if you're working on VS Code and want to debug in it rather than Chrome, then check my this answer

x
xanadont

Cmd+D from within the Simulator. It'll popup Chrome and from there you can use the Developer Tools.

Edit:

This is now linked in the help docs.


Using v8.2 simulator, Cmd+D seems to do nothing. Chrome is latest. Is some special plugin in chrome needed or a proxy?
Getting "Websocket debugger is not available. Did you forget to include RCTWebSocketExecutor?". :/
I had the following error: /Users/Serge/projects/serge/MyProject/node_modules/react-native/packager/launchChromeDevTools.applescript:668:671: script error: Expected class name but found property. (-2741). I found out that if I disabled sharing Windows applications with Mac in Parallels that it starts working. See stackoverflow.com/questions/29310936/….
That'll be Cmd+M on OS X and Android
Note that Cmd+D will do nothing if your Build Configuration is set to "Release"- make sure it is set to "Debug".
C
Community

Debugging React Native Apps

To debug the javascript code of your react app do the following:

Run your application in the iOS simulator. Press Command + D and a webpage should open up at http://localhost:8081/debugger-ui. (Chrome only for now) or use the Shake Gesture Enable Pause On Caught Exceptions for a better debugging experience. Press Command + Option + I to open the Chrome Developer tools, or open it via View -> Developer -> Developer Tools. You should now be able to debug as you normally would.

Optional

Install the React Developer Tools extension for Google Chrome. This will allow you to navigate the view hierarchy if you select the React tab when the developer tools are open.

Live Reload

To activate Live Reload do the following:

Run your application in the iOS simulator. Press Control + Command + Z. You will now see the Enable/Disable Live Reload, Reload and Enable/Disable Debugging options.


This answer basically copies the doc, which is a start, but not especially illuminating. What does "debug as you normally would" mean in this context?
Important note: React Developer Tools hasn't been working with React Native for months, and remains to be fixed. More info here: github.com/facebook/react-devtools/issues/229
Who said this is on mac?
@shinzou it's implied by the mention of a simulator (it's simulator for iOS devs, emulator for Android devs). Yes I know it's been a while, but still, just FWIW
that extension doesn't work with react-native. instead, you should see this: reactnative.dev/docs/debugging#react-developer-tools
J
John Lim

For an Android App, if you're using Genymotion you can toggle the menu by pressing CMD + m, but you may have to enable it in the menu by doing this.

https://i.stack.imgur.com/z3knE.png

Untick widget

Enable it by CMD + m click on debug in chrome


Finally found an Android solution, thanks! And in latest version, the debug in chrome is replaced with start debug remotely.
@MewX Could you please let us know how to do that ? 🤔
@Muhammed Hi, you could just follow cmd + m or ctrl + m, and select start debug remotely from the popup windows in your simulator
b
bpsourav21

if you want to debug using android device on Windows just open a command prompt then type ( make sure your adb working properly)

adb shell input keyevent 82

https://i.stack.imgur.com/ew83J.png

then select

debug JS Remotely

it will automatically open a new window.then open inspect element or press F12 for console.


is there an iOS equivalent to this command? I just started recently with RN and this would help a lot in the future
j
jedt

In addition to the other answers. You can debug react-native using the debugger statement

example:

debugger; //breaks execution

Your chrome dev tools must be open for this to work


F ya! That's what I needed! Thanks friend!
R
Rob

https://i.stack.imgur.com/cI0Ji.png

Try this program: https://github.com/jhen0409/react-native-debugger

Works on: windows, osx and linux.

It supports: react native and redux

You can also inspect the virtual component tree and modify styles that are reflected in the app.


Can I view value of specific variable via this debugger? I have tried console.log(url), but I cannot find where output is.
R
Ronan Boiteau

cmd ⌘ + D oddly didn't work for me. Pressing ctrl + cmd ⌘ + Z in the iOS simulator did kick off the debugging browser window for me.

This is the screen that pops up:

https://i.stack.imgur.com/QaJ8S.png

More details here.


I don't see these options... :(
C
Community

Debugging react-native 0.40.0 on Debian 8 (Jessie) can be done by navigating to http://localhost:8081/debugger-ui in Chromium or Firebug while your app is running in the android simulator. To access the in-app developer menu, run the following command in another terminal window, as pointed out here:

adb shell input keyevent 82


You can also do this by pressing D in your terminal running Metro.
C
Community

I don't have enough reputation to comment on the previous answers which are great. :) Here are some of the ways how I am debugging when developing react-native app.

Live reloading react-native makes it super easy to see your changes with the ⌘ + R keys or even just enable live reload and watchman will "refresh" the simulator with the latest changes. If you get an error, you can get a clue from the line number from that red screen. A couple of undo will get you back to working state and start again. console.log('yeah, seriously.') I find myself prefer letting the program run and logging some informations than adding a debugger break point. (tough debugger is useful when trying to work with external packages/libraries and it comes with autocompletion, so you know what other methods you can utilise.) Enable Chrome Debugging with debugger; break point in your program.

Well it depends on the type of errors you encountered and your preferences of how to debug. For most of the undefined is not an object (evaluating 'something.something'), method 1 and 2 will be good enough for me.

Whereas dealing with external libraries or packages written by other developers will require more effort to debug hence a good tool like Chrome Debugging

Sometimes it is coming from the react-native platform itself so googling for react-native issues will definitely helps.

hope this helps someone out there.


M
Michael Roland

Instead of Cmd+M, for Android Emulator Press F10 in Windows. The emulator starts to show all the react-native debug options.

https://i.stack.imgur.com/4mMfd.png


F10 not working in case of physical device
M
MJ Studio

React Native 0.62 Released - The Official solution is Flipper 🚀

Flipper is Android & iOS Mobile debugging tools without using debug mode in react native.

Since RN 0.62 (See this link), Flipper is initialized with default project.

Flipper has a few of plugins for debugging. The plugins include Layout, Network, Shared preferences

The greatest benefit of Flipper is not also many plugins but you can see Android / iOS device console debugging easily too.

The Flipper alert you about crash or network rejection too.

https://i.stack.imgur.com/YTUlu.png

Layout plugin includes accessibility mode and target mode.

https://i.stack.imgur.com/rXgJd.png

You can also see raw network request / response in your Application.


Do we have to manually install Flipper? Or is it shipped with React Native somehow? If so, how do we open it?
@Charanor I installed using react native upgrade guide when upgrading to 0.62
N
Naveen Vignesh

For Android: Ctrl + M (emulator) or Shake the phone (In Device) to reveal menu.

For iOS: Cmd + D or Shake the Phone to reveal menu

Make sure you have chrome.

On the revealed menu select Debug JS Remotely Option.

Chrome will be opened automatically at localhost:8081/debugger-ui. You can also manually go to debugger with this link.

There reveal console and you can see logs being noted.


n
nagasundaram I
adb logcat *:S ReactNative:V ReactNativeJS:V

run this in terminal for android log.


osx: adb logcat '*:S' ReactNative:V ReactNativeJS:V
adb logcat -s ReactNative:V ReactNativeJS:V
C
Community

https://i.stack.imgur.com/ggaHE.png

Dont forget to enable Debug JS Remotely in emulator if you using it.


W
Winter Soldier

To me the best way to debug on React-Native is by using "Reactotron".

Install Reactotron then add these to your package.json:

"reactotron-apisauce": "^1.1.2",
"reactotron-react-native-under-37": "^1.1.2",
"reactotron-redux": "^1.1.2", 

now, it just the matter of logging in your code. e.g.: console.tron.log('debug')


S
Sameera Chathuranga

Run your app in simulator - react-native run-ios Press ctrl + d and click on Debug JS Remotely

https://i.stack.imgur.com/GIKsF.png

webpage should open up at http://localhost:8081/debugger-ui , if not type the URL and go to this link in Chrome Right click on the page and click Inspect and it should open the developer tools for chrome

https://i.stack.imgur.com/4gK17.png

Go to the sources in the top menu and find your js class file in the right hand side file explorer You can put breakpoints to the view and debug the code in there as you can see in the image.


O
Olcay Ertaş

If you want to enable debugging by default:

import { NativeModules } from 'react-native';

if (__DEV__) {
  NativeModules.DevSettings.setIsDebuggingRemotely(true)
}

To get this working on Android:

npm install --save react-native-devsettings-android
react-native link react-native-devsettings-android

Reference: Launch a React Native app with “Debug JS Remotely” enabled by default


In specific place I should put those 4 lines? In App.js or index.js?
@JulienLamarche Put in App.js
that should be accepted as answer .... why too believe in commands .....
This is what I was looking for! Thanks.
J
Jinsung Sunwoo

First in your ios simulator, if you press [command + D] key then you can see this screen.

https://i.stack.imgur.com/XcXNj.png?s=256

Then click Debug JS remotely button.

After you might see React Native Debugger page like this.

https://i.stack.imgur.com/i9tF1.png

And then open your inspector[f12], and go to console tab debug it! :)


M
McG

By default, my ios simulator wasn't picking up the keystrokes which is why cmd-D didn't work. I had to turn on the settings for the keyboard using simulator's menu:

Hardware > Keyboard > Connect Keyboard

Now cmd-D launches chrome debugging.


S
Sujeesh Balan

For android app .Press Ctrl+M select debug js remotely it will open a new window in chrome with url http://localhost:8081/debugger-ui. You can now debug the app in chrome browser


A
Abhishek

Having a space in the file path prevents the Cmd+D from working. I moved my project to a location without a space and I finally got the Chrome debugger to work. Seems like a bug.


A
Abhijit Chakra

Very simple just two commands

For IOS $ react-native log-ios
For Android $ react-native log-android

this is the way that I do it!
This is a super great way to see what is going on with your store
P
Paul Roub

If you are using emulator use Ctrl+M & simulator Cmd+D Click on the - Debug js remotely Google Chrome go to console


N
Nkokhelox

https://i.stack.imgur.com/hVrUG.png

Then, try ⌘+m to pop up this dev settings dialog on Android emulator on a Mac.

If it doesn't show then go to AVD > (click the pen to edit your emulator configuration) > advanced settings > check the enable keyboard input box.

And then retry ⌘+m.

If it doesn't show still then go to the running emulator settings and on the Send keyboard shortcuts to combobox/dropdown then select the Emulator controls (default) option.

https://i.stack.imgur.com/hJc70.png

And then retry ⌘+m.

I hope this helps, it worked for me.


N
Nunchucks

If you're using Redux, I highly recommend React Native Debugger. It includes Chrome devtools, but also has Redux devtools and React devtools.

Redux Devtools: This allows you to view your actions, and step back and forth through them. It also allows you to view your redux store and has a feature to automatically diff the previous state with the updated state for each action, so you can see that as you step back and forth through a series of actions.

React Devtools: This allows you to inspect a certain component, namely all of it's props as well as it's component state. If you have a piece of the component state which is a boolean, it lets you click it to toggle it and see how your app reacts when it changes. Great feature.

Chrome Devtools Allows you to see all your console outputs, use breakpoints, pause on debugger; etc. Standard debugging features. If you right click the area where your actions are listed in Redux Devtools and select 'Allow Network Inspect', you can then inspect your API calls in the network tab of Chrome Devtools which is sweet.

In conclusion having these all in one place is fantastic! If you don't need one of them you can toggle it on/off. Get React Native Debugger and enjoy life.


j
jatin.7744

In React-Native debugging is much easier.

To debug in IOS use

cmd + d ctrl + cmd + z ( For simulator )

To debug in android

Shake device with touch ( Make sure your developer option is enable )


Nice explanation.
H
Hussam Kurd

You can use Safari to debug the iOS version of your app without having to enable "Debug JS Remotely", Just follow the following steps:

1. Enable Develop menu in Safari: Preferences → Advanced → Select "Show Develop menu in menu bar"
2. Select your app's JSContext: Develop → {Your Simulator} → Automatically Show Web Inspector for JS JSContext
3. Safari's Web Inspector should open which has a Console and a Debugger

H
Hardik Virani

This is the alternate way to use react native debugger application.

you can download application using below link it's very good application for managing redux store along with the source code.

react-native-debugger

as well now a days you may directly use below link for help you out.

chrome-developer-tools


A
Aishwarya

Step 1: Place debugger where ever you want to stop script, like:

  async saveItem(item, selectedValue) {
    debugger
    try {
        await AsyncStorage.setItem(item, selectedValue);
    }
    catch (error) {
        console.error('AsyncStorage error: ' + error.message);
    }
}

This will pause the debugger when ever control comes to this block of code.

Step 2: Press Cmd+D on ios emulator and Cmd+M on Android simulator. If you are having real device, shake the device to open dev menu, if you don't want to shake device follow this blog

Step 3: Select Enable Remote JS Debugging, this will open Chrome

https://i.stack.imgur.com/VAd25.png

Step 4: Select Developer Tools.

https://i.stack.imgur.com/wZkqU.png

https://i.stack.imgur.com/A9RdV.png

Place the debugger, wherever you wanna pause the script.

Enjoy debugging!!


Y
Youcef EL KAMEL

There is also a very good debuger name Reactotron. https://github.com/infinitered/reactotron

You don't have to be in debug mode to see some data value and there is a lot of option.

go have a look that is really usefull. ;)


Thank you! We were using RN 0.59 which didn't seem to be compatible with Flipper.