Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Require JS not loading if not at the root path

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 ;)

like image 548
maxwell2022 Avatar asked Jul 03 '14 01:07

maxwell2022


People also ask

Why is require not working in JS?

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.

Can I use require in JavaScript?

“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.

How does RequireJS load modules?

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.

How does RequireJS work?

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.


1 Answers

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>

Prefer to use this part :

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

like image 182
Sky Voyager Avatar answered Oct 06 '22 23:10

Sky Voyager