2

I'm running into a really frustrating problem in Node.js.

I'll start with what I'm doing.

I'm creating an object in a file and then exporting the constructor and creating it in other files.

My objects are defined like so:

File 1:

var Parent = function() {};

Parent.prototype = {
     C: function () { ... }
}

module.exports = Parent;

File 2:

var Parent = require('foo.js'),
      util = require('util'),
      Obj = function(){ this.bar = 'bar' };
util.inherits(Obj, Parent);
Obj.prototype.A = function(){ ... };
Obj.prototype.B = function(){ ... };
module.exports = Obj;

I'm trying to use the object like so in another file

File 3:

var Obj = require('../obj.js'),
      obj = new Obj();

obj.A(); 

I receive the error:

TypeError: Object [object Object] has no method 'A'

however when I run Object.getPrototypeOf(obj) I get:

{ A: [Function], B: [Function] }

I have no idea what I'm doing wrong here, any help would be appreciated.

4
  • What does util.inherits do? Does your code work if you omit the Parent thing (since A is a Obj-method)? Commented Feb 3, 2013 at 23:51
  • It's a Node.Js utility function API Doc Commented Feb 3, 2013 at 23:52
  • I'm not able to help you, i will come back if I find a solution. Commented Feb 4, 2013 at 0:48
  • It's worth noting that, in addition to my answer, if I copy and paste your source code, above (and fix the path issues and replace the ...s), I can run it with no issues. Commented Feb 4, 2013 at 0:52

1 Answer 1

5

I cannot reproduce your problem. Here is my setup:

parent.js

var Parent = function() {};

Parent.prototype = {
  C: function() {
    console.log('Parent#C');
  }
};

module.exports = Parent;

child.js

var Parent = require('./parent'),
    util = require('util');

var Child = function() {
  this.child = 'child';
};

util.inherits(Child, Parent);

Child.prototype.A = function() {
  console.log('Child#A');
};

module.exports = Child;

main.js

var Child = require('./child');
child = new Child();

child.A();
child.C();

And running main.js:

$ node main.js
Child#A
Parent#C

The source code is clonable via Git at the following Gist: https://gist.github.com/4704412


Aside: to clarify the exports vs module.exports discussion:

If you want to attach new properties to the exports object, you can use exports. If you want to completely reassign exports to a new value, you muse use module.exports. For example:

// correct
exports.myFunc = function() { ... };
// also correct
module.exports.myFunc = function() { ... };

// not correct
exports = function() { ... };
// correct
module.exports = function() { ... };
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for the info I'm afraid there might be something else wrong that I'm doing. I'll check out your example on my setup.
Okay I must be doing something else wrong since your code works fine in my environment.
what method do you use for what I'm trying to do?
@CalvinR I don't understand. What are you trying to do? I replicated your code in my answer, and it works. There must be a problem elsewhere. If you can explain your issue, I'd be happy to try to help.
turns out I know what I'm doing, I just don't know how to spell my problem was a case mismatch in what I was calling my function and how I was calling it, get vs Get :(
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.