Search code examples
javascriptdebuggingconsolerequirejsjs-amd

RequireJS in the browser: how can I log a variable without modifying my code first?


I'm using RequireJS in my browser. I have some JS loaded by a script tag, something like:

requirejs(["jquery", "shared", function($, shared) {
  var foo='bar';
}

I would like to print the current value of 'foo' from a JS console in the Chrome Dev Tools. How can I do this?

Edit 2: This question was originally very vaguely worded - sorry about that. To clarify, I do not wish to stop using RequireJS, pollute global, or know in advance what it is I want to debug.


Solution

  • Set a breakpoint in Chrome or Firebug to break at the point of your closure. Foo will then be available to the console until you resume script execution.

    edit: Scope will still matter. If the variable is private within a member of shared, you'll need to set a break in shared.js instead, e.g. if we assume shared.js contains:

    var shared = {
        myFunc: function() {
            var foo = 'bar';
            // break here
        }
    }