jedrzej.kurylo
jedrzej.kurylo

Reputation: 40899

Debugging Javascript/ReactJS errors

I'm building a small application with ReactJS and sometimes find it difficult to debug it.

Every time I make some Javascript error, like missing let/var in front of new variable, missing require for a component that I later use, my application just stops working (the code does not execute beyond the line where the error is), but I'm not getting any errors in browser's console. It seems as if some ReactJS code was intercepting errors, maybe handling them some custom way. Is there anything like that in ReactJS? How can I see errors in the console?

I'm using gulp/gulp-connect/browserify set to run the application.

Let me know if you need any additional data or code samples, I'll update the question.

Upvotes: 8

Views: 3416

Answers (5)

Mitsutoshi Watanabe
Mitsutoshi Watanabe

Reputation: 301

I suffered from the similar problem with this like missing let/var, require, and other trivial mistakes inside of the react context. In my case, the cause is the mistake of Promise statement. Promise seems to suppress any exceptions that occur after it is called. I could resolve the problem by handling exception like below.

var Promise = require('es6-promise').Promise;
var promise = new Promise(...)
promise
  .then(function(data){...})
  .catch(function(e) {
    console.error(e.stack)
}

Upvotes: 1

Taku
Taku

Reputation: 5918

react-slingshot is a starter kit and it shows error at compile time and shows stack trace on the browser. It also has testing set up.

Upvotes: 0

pherris
pherris

Reputation: 17693

I'm guessing that the incorrect JS syntax is causing your gulp process to fail which would result in your application not being bundled/deployed in the browser at all.

It seems like there should be errors in your system console (where gulp is running) - as opposed to your browser console. Possibly your gulp process crashes when this happens too. If there are no errors in your console, you may have to listen for them. This post has an example of how to log errors from browserify:

gulp.task('browserify', function(){
  var b = browserify();
  b.add('./main.js');

  return b.bundle()
    .on('error', function(err){
      console.log(err.message); //
      this.end();
    })
    .pipe(source('main.out.js'))
    .pipe(gulp.dest('./dist'));
});

Probably the best solution is to run your code through jshint before browserify so that you aren't trying to browserify syntactically invalid code.

Caveat: not a current gulp user

Upvotes: 1

Yevgen Safronov
Yevgen Safronov

Reputation: 4033

Usage of React Hot Loader which is a Webpack plugin should solve most of the problems you have in a development process. It's easy to integrate into existing project and has quite a few examples how to put all the things together.

As a result:

  • your code changes would be pushed to the browser
  • in case of the error you will have meaningful stack trace in the browser console.

Upvotes: 1

Felix Kling
Felix Kling

Reputation: 816232

If you know that an error is thrown but swallowed by some other code, you can enable in your browser's debugger to pause execution when an exception is thrown, even if it is caught somewhere:

enter image description here

Note however that libraries sometimes deliberately trigger exceptions while testing whether the browser supports certain features. You'd have to step over those.

Upvotes: 6

Related Questions