javascriptrequirejsdynamic-script-loading

Require.js -- how to load relative to a module directory


My structure looks like so:

so in /modules/app/main.js, I want to load in the /modules/app/views/app.js but having problem

Here is /modules/app/main.js

define(['views/app'],function(App){
console.log('app main')
//var app = new App();
})

The console error message I get is Get failed with path GitProjects/GameApp/views/app.js

How do I get it to load relatively inside modules?

here is ./main.js the file that holds the configs

require.config({
    paths: {
      jquery: './libs/jquery-2.0.3',
      underscore: './libs/underscore',
      backbone: './libs/backbone'
    },
    shim: {
        "underscore": {
            exports: '_'
        },
        "backbone": {
            deps: ["underscore", "jquery"],
            exports: "Backbone"
        }
    }
}); 

and here is my index.html

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Stop Game App</title>
        <script src="./libs/require.js" data-main='./main'></script>
    </head>
    <body>
        <div id="app"></div>
        <script>
        require(['./modules/app/main'],function(){
                console.log('main')
            })
        </script>
    </body>
</html>

Solution

  • You just need to set the path to "./" and be relative. That's all there is to it.