tnhu / jsface

Small, fast, elegant, powerful, and cross platform JavaScript OOP library. Support main(), singleton, super call, private, mixins, plugins, AOP and more.
MIT License
301 stars 46 forks source link

Undefined is not an object | Exporting a Node.js Module #18

Closed hightechlofi closed 10 years ago

hightechlofi commented 10 years ago

I have a node module (Output.js) that I wrote with jsface as follows:

module.exports = Output;

var Output = Class({

code, etc...

})

In the app.js I use is as follows:

var Output = require("./Output");

var output = new Output(config);

When I run app.js I get the following error:

/var/lib/brewbone/bb_hwc/test_output.js:23 var output = new Output(config); ^ TypeError: undefined is not a function at Object. (/var/lib/brewbone/bb_hwc/test_output.js:23:14) at Module._compile (module.js:456:26) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Function.Module.runMain (module.js:497:10) at startup (node.js:119:16) at node.js:902:3

As I am a noob, I assume I have done something stupid, but for the life of me I cannot pinpoint it. Any suggestions?

tnhu commented 10 years ago

Not sure what you are doing. Maybe your order is not right. Try this:

npm install jsface
Output.js

var jsface = require("jsface"),
    Class  = jsface.Class;

var Output = Class({
  sayHi: function() {
    console.log("Hello World");
  }
});

module.exports = Output;
app.js

var Output = require("./Output");

var output = new Output();

output.sayHi();

Run it:

node app.js
hightechlofi commented 10 years ago

figured it out, noob mistake. Per your example, the module.exports has to go at the end, I had it at the beginning.