define is not defined errot

793 views
Skip to first unread message

Reza Razavipour

unread,
Nov 26, 2013, 7:36:41 PM11/26/13
to nod...@googlegroups.com
so I am trying to export a class constructor... This is what I have done based on my readings.

define('SystemVersionResponse', function() {

    

function SystemVersionResponse(input) {

         this.errorMessage = String.fromCharCode.apply(String, input.rsp.rsp.errorMessage.item).replace(/\0/g,'');

    this.errorNumber = input.rsp.rsp.errorMessage.errorNumber;

    }


    SystemVersionResponse.prototype.getErrorCode = function() {

    return errorNumber;

    };


    SystemVersionResponse.prototype.getErrorMessage = function() {

    return errorMessage;

    };


    SystemVersionResponse.prototype.getRevision = function() {

    return revision;

    };

    return SystemVersionResponse;

});

I get the syntax error:

ReferenceError: define is not defined

What package am I missing?


Is this the correct way to export a constructor? 

Alex Kocharin

unread,
Nov 26, 2013, 7:55:14 PM11/26/13
to nod...@googlegroups.com

You're missing an AMD loader. Node.js module system is based on a CommonJS, that's a different module standard, so while your approach is correct, but won't work by default.

Ryan Schmidt

unread,
Nov 26, 2013, 10:16:38 PM11/26/13
to nod...@googlegroups.com
The nodejs way would be:


function SystemVersionResponse(input) {
// the rest of your function here
}

exports.SystemVersionResponse = SystemVersionResponse;

Alex Kocharin

unread,
Nov 27, 2013, 12:47:19 AM11/27/13
to nod...@googlegroups.com

It is not a nodejs way, it's a way that default loader works (there are alternate loaders, and there is ES6 module system, though we don't know yet what impact it would have). So there is a difference.

There is one reason why it is not a node.js way. It blocks. In fact, one of the reasons why node.js is so more popular than all other server-side js frameworks, and why we are all here laughing out loud at silkjs, is an async IO. But here it is synchronous require that uses readFileSync (which should've been deprecated ages ago), and flies in the face of everything node.js stands for.

So yeah, it is rarely cause any trouble, and most of the modules are written in this way, but it doesn't make it nodejs way.

Ryan Schmidt

unread,
Nov 27, 2013, 4:26:57 AM11/27/13
to nod...@googlegroups.com
That’s an interesting opinion, but all nodejs code I’ve ever read makes modules this way, so that makes it the de-facto nodejs way.

Floby

unread,
Nov 28, 2013, 4:43:39 AM11/28/13
to nod...@googlegroups.com
That's an interesting opinion, and I suggest you read more of this mailing list archives because this has been brought up many times with always the same answer.

It is the CommonJS way, which node.js has adopted early on. It is therefore also the node.js way.

If the OP wants his module to work in the 5 next minutes, he only needs to know this :

function myConstructor() {
}

module.exports = myConstructor


Reply all
Reply to author
Forward
0 new messages