I have an issue with my setup of requirejs, I've tried to fix it but each time I'm breaking the app. Here is my index.html
<script type="text/javascript" src="/js/bower_components/requirejs/require.js"></script>
<script>
// obtain requirejs config
require(['require', 'js/require-config'], function (require, config) {
// update global require config
window.require.config(config);
// load app
require(['main']);
});
</script>
This is working well if I load the app from the root path "/" but as soon as I try to refresh the app somewhere else (ie. /user/1) I got the following error:
Resource interpreted as Script but transferred with MIME type text/html: "http://domain.local/users/js/require-config.js".
require.js:1895 Uncaught SyntaxError: Unexpected token <
require-config.js:1 Resource interpreted as Script but transferred with MIME type text/html: "http://domain.local/users/main.js".
require.js:1895 Uncaught SyntaxError: Unexpected token <
as you can see it's looking for the file at the relative path:
http://domain.local/users/js/require-config.js
but it should load:
http://domain.local/js/require-config.js
if I add a /
in front of the module, then it's not working anymore because it's looking for a file instead of a module:
<script type="text/javascript" src="/js/bower_components/requirejs/require.js"></script>
<script>
// obtain requirejs config
require(['require', '/js/require-config'], function (require, config) {
// update global require config
window.require.config(config);
// load app
require(['/js/main']);
});
</script>
Resource interpreted as Script but transferred with MIME type text/html: "http://domain.local/js/require-config". require.js:1895
Uncaught SyntaxError: Unexpected token < require-config:1 Resource interpreted as Script but transferred with MIME type text/html:
"http://domain.local/js/main". require.js:1895 Uncaught SyntaxError: Unexpected token <
I've tried a couple of combinaison, but each time it's breaking something :(
Help would be greatly appreciated ;)
This usually happens because your JavaScript environment doesn't understand how to handle the call to require() function you defined in your code. Here are some known causes for this error: Using require() in a browser without RequireJS. Using require() in Node.
“Require” is built-in with NodeJS With require , you can include them in your JavaScript files and use their functions and variables. However, if you are using require to get local modules, first you need to export them using module.
RequireJS uses Asynchronous Module Loading (AMD) for loading files. Each dependent module will start loading through asynchronous requests in the given order. Even though the file order is considered, we cannot guarantee that the first file is loaded before the second file due to the asynchronous nature.
RequireJS loads each dependency as a script tag, using head. appendChild(). RequireJS waits for all dependencies to load, figures out the right order in which to call the functions that define the modules, then calls the module definition functions once the dependencies for those functions have been called.
If you path start with "/", add .js extension.
<script type="text/javascript" src="/js/bower_components/requirejs/require.js"></script>
<script>
// obtain requirejs config
require(['require', '/js/require-config.js'], function (require, config) {
// update global require config
window.require.config(config);
// load app
require(['/js/main.js']);
});
</script>
index.html :
<script data-main="/js/require.file.js" src="/js/bower_components/requirejs/require.js"></script>
require.file.js :
require.config({ // Your config :
baseUrl: "/js"
});
require(["main"],
function(someModule, myModule) {
//onload.
}
);
See more : Configuration Options
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With