Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js include class file

I got 2 files:

start.js

    var ConversationModule = require('./src/classes/conversation/Conversation.js');
    ConversationModule.sayhello();

conversation.js

    var ConversationModule = new Object();

    ConversationModule.sayhello = function () {
    console.log("hello");
    };

    exports.ConversationModule = ConversationModule();

In start.js I cannot call the sayhello() method. I get following error

TypeError: object is not a function

I just don't get it why it doesn't work - I'm new to node :)

like image 348
daniel7558 Avatar asked Oct 19 '13 19:10

daniel7558


1 Answers

You are trying to export ConversationModule as a function, which it is not. Use this instead:

exports.ConversationModule = ConversationModule;

Since you're also assigning the variable as a property of exports, you'd have to call it like this:

var ConversationModule = require('./file').ConversationModule;
ConversationModule.sayhello();

If you don't want to do that, assign the object to module.exports:

module.exports = ConversationModule;

And call it like this:

var ConversationModule = require('./file');
ConversationModule.sayhello();
like image 53
hexacyanide Avatar answered Nov 15 '22 10:11

hexacyanide