I rely heavily on React Native's "Debug in Chrome" feature and hence on Chrome's debugger. But I've noticed a huge flaw with this system: modules that I import using an ES6-style import are not visible in the scope in Chrome even though the code executes fine. This makes debugging code using these import statements very difficult.
If I replace an import
statement with a var MyModule = require(...)
then the module is visible in the scope.
After reading ES6 module import is not defined during debugger I took a look at the transpiled source code produced by React Native (by loading http://localhost:8081/index.ios.bundle?platform=ios&dev=true
in my browser) and noticed that the module in question is loaded under a different name:
var _MyModule = require('MyApp/MyModule.js');
var _MyModule2 = babelHelpers.interopRequireDefault(_MyModule);
and indeed I can use _MyModule2
in Chrome. I have a couple of related questions:
import
statements easier in Chrome with React Native? For instance, I'm used to just moving my mouse over a variable in Chrome to see the value, but this is no longer possible with these imports. (Debugging with chrome with es6 suggests enabling #enable-javascript-harmony
in Chrome and turning off source maps but given the Flow code and uglification I doubt this would work well.)Thank you.
To debug the JavaScript code in Chrome, select "Debug JS Remotely" from the Developer Menu. This will open a new tab at http://localhost:8081/debugger-ui. Select Tools → Developer Tools from the Chrome Menu to open the Developer Tools.
Start debugging Set the breakpoints in the JavaScript code, as required. Open the HTML file that references the JavaScript to debug or select the HTML file in the Project tool window. From the context menu of the editor or the selection, choose Debug <HTML_file_name>.
To use Chrome's DevTools with React Native, first make sure you're connected to the same Wi-Fi, then press command + R if you're using macOS, or Ctrl + M on Windows/Linux. When the developer menu appears, choose Debug Js Remotely . This will open the default JS debugger.
Connecting a React Native app to React Native Debugger To connect your app with React Native Debugger, you need to run your app and start debug mode. To start debug mode, shake your mobile device or press Command + Shift + Z or Ctrl + M and choose the debug option. We're all set to begin using React Native Debugger.
I have enabled Chrome experimental features in Chrome flags and I have no problem using an ES6-style import. If you haven't, type chrome://flags
in your address bar and look for Experimental JavaScript
. This should solve your problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With