cujojs / curl

curl.js is small, fast, extensible module loader that handles AMD, CommonJS Modules/1.1, CSS, HTML/text, and legacy scripts.
https://github.com/cujojs/curl/wiki
Other
1.89k stars 216 forks source link

For anyone facing the error : Multiple anonymous defines encountered #283

Closed NotSqrt closed 9 years ago

NotSqrt commented 9 years ago

My pages have been facing "Multiple anonymous defines encountered" irregularly for some time, and I have finally understood why :

I have set up a minimalistic page to demonstrate the error : http://jsbin.com/suhehuwita/1/ This page triggers the error very reliably, but in a real world web page with more modules, it would happen randomly.

Basically, if an AMD-compatible module is loaded after curl, outside a call to curl(), the error will be triggered.

What you should not do:

<html>
    <head>
        <script type="text/javascript">
            curl = {/* curl config */};
        </script>
        <script src="/curl.js"></script>
        <script src="/some-amd-module.js"></script> <!-- after curl -->
    </head>
    <body>
        <script type="text/javascript">
            curl("same-amd-module", function () {});
        </script>
    </body>
</html>

What you should do instead:

<html>
    <head>
        <script type="text/javascript">
            curl = {/* curl config */};
        </script>
        <script src="/some-amd-module.js"></script> <!-- before curl -->
        <script src="/curl.js"></script>
    </head>
    <body>
        <script type="text/javascript">
            curl("same-amd-module", function () {});
        </script>
    </body>
</html>
unscriptable commented 9 years ago

Thanks @NotSqrt! This is a common problem encountered by people mixing AMD loaders and <script> elements that might load AMD modules. I'm glad you figured it out and left a solution for others! -- J