Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dudumanbogdan/6fb279fc555c8f3eec73537160dfcd59 to your computer and use it in GitHub Desktop.
Save dudumanbogdan/6fb279fc555c8f3eec73537160dfcd59 to your computer and use it in GitHub Desktop.
Some nice tricks for troubleshooting RequireJS modules: http://tech.pro/blog/1561/five-helpful-tips-when-using-requirejs

Some Tricks for Troubleshooting

You can use these API calls in your code if you need to, but I've actually found them quite useful when on the console in Chrome:

require.defined(moduleId) - returns true if your moduleId has been defined and is ready for use.

require.specified(moduleId) - returns true if your moduleId has been listed as a dependency by another defined module. Note that just because this returns true doesn't mean your moduleId is ready to use (don't you just love asynchrony?).

requirejs.s.contexts._.config - I learned about this from Vernon Kesner. This is technically a "back door/undocumented" call - so it could change or disappear without warning. However it returns a very useful object full of configuration info, see below: Chrome Console results for requrejs.s.conects._.config

This is the result of calling requirejs.s.contexts._.config inside a sample gif-generation app I used to demo Web Workers at Devlink. You can see all the relevant configuration data: the base URL, the paths we've mapped, the shim configuration, etc.

Two other key items to know about when it comes to troubleshooting RequireJS are 'errbacks' and the requirejs.onError method:

RequireJS "errbacks"

When you make a require call, you can include a third argument - a callback that receives an error argument, allowing you to react to the error, instead of ultimately generating an uncaught exception. The method signature, when using "errbacks" looks like this:

require(
    [ "backbone" ], 
    function ( Backbone ) {
        return Backbone.View.extend({ /* your magic here */ });
    }, 
    function (err) {
        /* 
            err has err.requireType (timeout, nodefine, scripterror)
            and err.requireModules (an array of module Ids/paths)

            Inside here you could requirejs.undef('backbone') to clear
            the module from require locally - and you could even redefine
            it here or fetch it from a different location (though the
            fallback approach earlier takes care of this use-case more succinctly)
        */
    }
);

requirejs.onError

RequireJS has a global onError handler that will catch any errors not already handled by "errbacks". To use it, simply set it like this:

requirejs.onError = function (err) {
    /* 
        err has the same info as the errback callback:
        err.requireType & err.requireModules
    */
    console.log(err.requireType);
    // Be sure to rethrow if you don't want to
    // blindly swallow exceptions here!!!
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment