1
votes

I have an issue with loading module using requireJS. I've got two files:

File1: app1/js/utils/commons/commons.js

define("utils/commons/commons",
    [
        "../../../../app2/MenuModule"
    ], function (MenuModule) {
//MenuModule is undefined here
var app = angular.module('commons', ['MenuModule']);

});

File2: app2/MenuModule.js

define("../../../../app2/MenuModule",
    [
        "../../../../app2/MenuController",
        "../../../../app2/MenuRestProvider"

    ], function (MenuController,
    MenuRestProvider) {

var app = angular.module('MenuModule', []);

app.factory('MenuRestProvider', MenuRestProvider);
app.controller('MenuController', MenuController);

return app;

});

The point is: File1 loads File2, and 'define' function from File2 is run. Dependencies from File2 are not fetched, and function passed to 'define' function is not evaluated. Can you see what causes this problem?

1
Please update the question with the errors from the console. And I can see several strange things: (1) Using named modules, (2) Using ../ in the name of the module (this is most probably the culprit, I bet) (3) Long ../ chains (really fragile) and (4) Suspicious dependency paths in file2 (shouldn't "../../../../app2/MenuController" simply be "./app2/MenuController" or even "app2/MenuController"?)Nikos Paraskevopoulos
The strangest thing is I don't get any errors in console from requireJS. According to point 4, as far as I know, all paths should be relative to baseUrl defined in require.config.ozgus

1 Answers

1
votes

I think you are confusing paths with module IDs. It's easy to confuse one with the other, since paths ultimately lead into defining the moduleID based on how it's resloved, but when you put relative symbols in a module reference, don't think about 'going up a path', but rather 'moving up the module hierarchy.

All this means is that you need to share your paths config with us so that we can see how the referenced module IDs could be translated into the paths for loading.

Also agree: putting the moduleID directly in the define() call has code smell. You should be letting the amd loader define the moduleIDs for you (by making them anonymous modules.