Tim Nuwin
Tim Nuwin

Reputation: 2887

Undefined is not a function - RequireJS

I'm having trouble including a module in RequireJS.

There's two files.. test.js and card.js

In test.js, when the page loads it says "undefined is not a function":

require(
    ['app', 
     'jquery', 
     'card'], 
    function(App, $, Card) {
        var card = new Card("test");
    }
);

Here is the card.js:

define("Card", function () {
    function Card(name) {
       this.name = name;             
    };

    return Card;
});

I put some console.log()'s in the card.js and it calls those fine when it's being referenced like that in test.js. Also if I were to define a regular js object class in card.js (e.g.):

function Card(name) {
   this.name = name;
}

I am able to create that Card object properly in test.js.

Any clues how I'm hooking it all up wrong?

Upvotes: 0

Views: 287

Answers (1)

medimatrix
medimatrix

Reputation: 137

The problem, as Bergi said, is that your module names are not the same.

To RequireJS, Card and card are different modules.

All you have to do is change card.js to

define("card", function () {
    function Card(name) {
       this.name = name;             
    };

    return Card;
});

Upvotes: 1

Related Questions