Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get code of specific line in Javascript file

When catching an error, I want to get with the stack trace the actual code of the line that triggered the error. Given that I have the path to the file and the line number throwing the error, what's the best way to also get the actual code of that line?

like image 803
Omri Avatar asked Jul 02 '26 00:07

Omri


1 Answers

Beyond the obvious of "looking it up in the file", there are a few things you can do to get it automatically. Checkout the Tracekit project on GitHub. For errors it captures, it will do an AJAX request for the script and find the relevant lines in the text.

Alternatively, if you're looking for a way to handle this automatically, you should consider a service like TrackJS that will capture all the relevant scripts and apply sourcemaps for you. I am one of the original developers and I've used it on many projects to fix bugs ridiculously fast :)

like image 61
Todd Gardner Avatar answered Jul 04 '26 13:07

Todd Gardner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!