1

I am trying my might at doing a NODE JS project, and learning the in's and outs of the Node JS structure. I have looked all over the internet to find out how to do this with little or no luck. The problem is as follows:

I have a class called player, which was created in a file called player.js

var playa = function ()
 {

       this.createPlayer = function(){

       }

 }

In my index.js file I link my player

      var playa = require("./player.js");

however, when I try to reference my player

  io.on('connection', function(socket){
       var player =   new playa();
        player.createPlayer();
});

the console tells me that playa is not a function and the server stops.

  1. Am I even allowed to use traditional OOP JS in node? If I move this class to the index.js file it works splendidly, however it will get sloppy since the class is large and I plan on using at least 5 classes.

2.If it is possible too structure my objects this way, how do I link them/reference them correctly?

  1. if neither or possible, what would be the correct way to structure my objects? I am hoping not to get away from my current convention since that is what I am used to and I need to create multiple instances of player.
3
  • I don't think you're properly exporting the symbol from your module. Commented Sep 3, 2014 at 13:43
  • 2
    Currently you do not export anything (or undefined as a default value) from your player.js module. Hence the error. Commented Sep 3, 2014 at 13:45
  • I tried using module export.. When I did cosnole told me I had a type error and playa was not a function @Sirko Commented Sep 3, 2014 at 13:48

2 Answers 2

4

If you export the constructor as a property of module.exports, then you'll have to refer to it by that as a property of the value returned from require():

var playa = require("./player.js");
var player = new playa.playa();

If you want require() to return the function itself, you can write

module.exports = function()
{
   this.createPlayer = function(){

   }
}
Sign up to request clarification or add additional context in comments.

3 Comments

So first i have to reference the namespace ie the export than the underlying class. Jeez i feel sheepish,. Thank you @Pointy
@Dnaso think of it this way: the return value of require() is whatever the value of module.exports ended up being when the imported module runs. It starts off as a simple empty object, but you can make it be whatever you want - in this case, a function.
interesting. I kind of like the way Node makes you structure your scripts.
3

Add this to the bottom of your player.js file

module.exports.playa = playa;

This will allow that "class" to be available

EDIT

You will need to create an instance like the following:

var p = require('./player.js')
var player = new p.playa();

3 Comments

var player = new playa(); ^ TypeError: object is not a function
that was the console output, followed by my server crashing
@tier1 Currently you have a mistake with upper/lower case letters for the function. You export as lowercase, but use as uppercase.

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.