How can I debug a minified JS in firebug?

JavascriptDebuggingFirefoxFirebug

Javascript Problem Overview


I have a web page which includes insane amount of minified JS files. The web page works perfectly fine on my local network but throws some JS error on staging. There is an issue in JS and I wan't to debug it. When I load the JS in Firebug's script tag it appears in one long horizontal line. Is there a way out in Firebug that expands or beautifies the script for debugging? I know I can use jsbeautifier but they wont help me. I can not upload an expanded file to CDN, defeats the purpose of using CDN.

Points to be noted,
a) I can not control the box which serves JS, its on CDN, I mentioned it.
b) I can use beautifiers etc but would that help me in debugging the script in run time? IMHO, no
c) Being bound by NDA and other legal things I can not share the script but its a generic problem, you can encounter it with a minified jQuery

Javascript Solutions


Solution 1 - Javascript

  1. Beautify your script
  2. Add the CDN host in /etc/hosts or your local DNS to resolve it to your own web server
  3. Host the beautified version and everything that you need on said web server
  4. Both Firefox and Chrome (versions as of this edit) support beautifying script with the {} button available in the inspector.

Solution 2 - Javascript

Just load the minified file and press the {} button at the bottom and it instantly beautifies, making breakpoints and other debugging possible. (True for Chrome too)

Solution 3 - Javascript

This is a common problem and the Chrome dev team have recently come up with an elegant solution, which they've called Source Maps - see http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/ for more info, I think you'll find it's exactly what you (and the rest of us) have been crying out for! :)

Solution 4 - Javascript

This is more a workaround, but it can help. The idea is that we will replace files coming from the server by files on your machine.
This will work with any browser.
It takes a bit of setup the first time (15 minutes maybe), but then it can be very convenient.
It can also helps testing your bug-fixes in a live/prod environment.

  1. Get Fiddler (it's a web debugging proxy), install it, run it.
    http://www.fiddler2.com/fiddler2/
    (Restart browser after install to get the Fiddler extension)
  2. If you debug an HTTPS website, check this first:
    http://www.fiddler2.com/Fiddler/help/httpsdecryption.asp
  3. From now on, you should see in Fiddler ("Web Sessions" pane on the left) all downloads made by your browser, including JS files.
    If not, check this : https://stackoverflow.com/questions/3444352/fiddler-not-displaying-sessions
  4. Find the file you want to debug in the list (Ctrl+F works)
  5. Click on the file. Then either:
    • get the file content from the inspectors pane (textView tab), beautify it, save to a file on your local computer
    • or have access to a file which contains the source code (ex: from your source control)
  6. Go to AutoResponder tab (top left pane).
    Select "Enable automatic responses" checkbox.
    Select "Unmatched requests passthrough" checkbox.
  7. Drag your file from left pane to right pane (prefills rule editor at the bottom)
  8. Set the other field with the path of your local file
  9. Click the Save button
  10. Reload the page and enjoy your debugging session.

Fiddler can do many more things, but this use-case answers the initial question.

Solution 5 - Javascript

Consider a Change!

Firefox w/ Firebug was my favorite JavaScript debugging method for almost a year, but I've recently moved to Google-Chrome's Developer-Tools which is far more superior.

  • Chrome supports an On-The-Fly (built-in feature) beautification of JavaScript resources enter image description here

  • Once beautified, you are free to debug the JavaScript resource file, as it was "natively" downloaded beautified from the web-server. Breaking-points are set by clicking the line number. enter image description here

  • One of the most extremely powerful feature,
    Is once You've Stopped In A Breaking-point, You Are Free To Execute Commands (using console) In The Same Scope You ARE In The Breaking-point. In Firefox you can't do that. enter image description here Its so easy to debug (even anonymous functions), You'll never be back to Firefox.
    Try It!

Solution 6 - Javascript

Pretty-print your JavaScript. Google this and you'll find multiple on-line JS beautifiers.

I happen to use http://jsbeautifier.org/ myself and it works fine, but search for others and use one that suits your needs.

Caveat: You still won't be able to get meaningful local variable names (which are usually renamed by a minifier). If the code was compiled by the Closure Compiler, then you absoutely won't get any useful information back at all, even when beutified, because then all variables and functions and properties are mangled (not only local ones).

Now, if your problem is with debugging code that comes from outside (e.g. a CDN), obviously that code would be minified, and you can't save your beautified version back there. In this case, you can replace the

Solution 7 - Javascript

Placing breakpoints on JavaScript makes debugging much easier, but if your code has already made it to production then it's probably been minified. How can you debug minified code? Helpfully, some of the browsers have an option to un-minify your JavaScript.

In Chrome and Safari, simply select the 'Scripts' tab, find the relevant file and then press the "{ }" (pretty print) icon located in the bottom panel.

In Internet Explorer, click the tool icon by the script selection drop down to find the option to format the JavaScript.

Opera will automatically prettify minified JavaScript. Source

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKumarView Question on Stackoverflow
Solution 1 - JavascriptrspView Answer on Stackoverflow
Solution 2 - JavascriptKyle HeironimusView Answer on Stackoverflow
Solution 3 - JavascriptMarcusTuckerView Answer on Stackoverflow
Solution 4 - JavascriptJB HurteauxView Answer on Stackoverflow
Solution 5 - Javascriptuser257319View Answer on Stackoverflow
Solution 6 - JavascriptStephen ChungView Answer on Stackoverflow
Solution 7 - JavascriptAamolView Answer on Stackoverflow