Rajish
Rajish

Reputation: 6805

Why RequireJS truncates '.js' from the main module name and cannot find the file afterwards?

This happened to me while using the Play! framework, but I think it could happen generally when using RequireJS.

In t he template I have used the statement:

 <script>require = {
      // The path where your JavaScripts are located
      baseUrl: "assets/js/",
    ...
};</script>
@helper.requireJs(core = routes.Assets.at("js/require-jquery.js").url, module = routes.Assets.at("js/main.js").url)

Which is compiled to:

<script> require = ... </script>
<script type="text/javascript" data-main="/assets/js/main.js" src="/assets/js/require-jquery.js"></script>

When I hit reload in the browse I can see the following errors in the console:

GET http://localhost:9000/assets/js/main?bust=1361232944505 404 (Not Found) require-jquery.js:1843
Uncaught Error: Script error
http://requirejs.org/docs/errors.html#scripterror 

As you can see the file requested has the extension .js truncated. Why? How to avoid that?

Upvotes: 2

Views: 521

Answers (1)

Rajish
Rajish

Reputation: 6805

The problem is that the RequireJS config contains the baseUrl field, and the same path is repeated in the data-main attribute of the script tag. Changing the Play! framework helper statement to:

@helper.requireJs(core = routes.Assets.at("js/require-jquery.js").url, module = "main")

or in general case:

<script type="text/javascript" data-main="main" src="/assets/js/require-jquery.js"></script>

removes the problem.

Why that happens and manifests that vague way is the question to the RequireJS developers. Anyway the Play! framework sample described in the documentation here or here should be corrected, and the common errors section on the RequireJS page should also be updated.

Upvotes: 1

Related Questions