Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging ES6 import statements with React Native in Chrome

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:

  1. Source maps seem like an amazing technology! Why don't they support mapping variable names too?
  2. Is there any way to make debugging with 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.

like image 358
Lane Rettig Avatar asked Jan 21 '16 14:01

Lane Rettig


People also ask

How can you debug a React Native application directly in your Chrome browser?

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.

How do I debug JavaScript embedded in Chrome?

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>.

How do I run React Native code in Chrome?

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.

How do I connect React Native app to 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.


1 Answers

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.

like image 135
Marzieh Bahri Avatar answered Oct 14 '22 11:10

Marzieh Bahri