Can't access dependency object fields in require.js - Cannot read property of undefined

382 views Asked by At

I have created module

myModule.js

require(['jquery'],function($){
    return {
        init: function(){
            $('.test').hide();
        }
    }
});

config:

require.config({
    baseUrl : "js/",
    paths : {
        jquery : 'lib/jquery/jquery-2.1.1',
        myModule : 'lib/modules/myModule
    },
    shim : {

    }
});

Now I want to use mymodule.init function in other module in proper place (lets say after AJAX load)

require(['jquery', 'myModule'],function($, myModule){
    ...
        myModule.init();
    ...
});

Here I have an error:

Cannot read property 'init' of undefined 

What I did wrong?

1

There are 1 answers

1
Louis On BEST ANSWER

In myModule.js you call require. You should call define instead so that your module is defined. A define call with a first argument that is a dependency list and a second argument that is a function is somewhat like a require call with the same arguments but additionally defines your module.