0

I'm struggling to understand what I am missing here:

FILE: Sprite.js
function Sprite() {
}

Sprite.prototype.move = function () {
}

module.exports = Sprite;

FILE: Ship.js
function Ship() {
}

Ship.prototype = new Sprite();

Ship.prototype.enable = function() {
}

FILE: Server.js
var util    = require('util'),
io  = require('socket.io'),
Sprite  = require('./sprite.js'),
Ship    = require('./ship.js');

var boo = new Ship(Sprite);

Outside of Node.js this works fine. In Node.js however it won't recognise Sprite in the ship file. I've tried using module.export = Sprite at the end of the sprite file with no success.

Cheers

Chris Evans
  • 993
  • 2
  • 13
  • 30

2 Answers2

4

Export Sprite in FILE: Sprite.js like this :

function Sprite() {
}

Sprite.prototype.move = function () {
}
exports.Sprite = Sprite;

Then inside FILE: Ship.js ( this is the tricky part you're missing ) use require to require the Sprite like this:

var Sprite = require('/path/to/Sprite');
function Ship() {
}

Ship.prototype = new Sprite();

Ship.prototype.enable = function() {
}

If a module exports smth, if you whant to use it then you need to require it (in the module you're trying to play with it, not in the main module) don't you?, how else is nodejs going to know where the ship ''class'' is ? more info here


Edit, see this working ( all files need to be in the same directory or you'll need to change the require path )

File sprite.js :

var Sprite = function () {
}
Sprite.prototype.move = function () {
    console.log('move');
}
module.exports = Sprite;

File ship.js :

var Sprite = require('./sprite');
function Ship() {
}

Ship.prototype = new Sprite();

Ship.prototype.enable = function() {
    console.log('enable');
}

module.exports = Ship;

File main.js :

var Ship = require('./ship');

var boo = new Ship();

boo.move();
boo.enable();

Run the example using node main.js and you should see :

C:\testrequire>node main.js
move
enable
Community
  • 1
  • 1
Poelinca Dorin
  • 9,577
  • 2
  • 39
  • 43
  • Ok if I used require() in Sprite file on it's own, it still says sprite is not defined. If I use require() in both sprite and server it says "cannot set property of move of undefined". – Chris Evans Jan 04 '12 at 10:43
  • You need to make shure you're exporting correctly the Sprite constructor ( use console.log ), then in Ship module import it using require. – Poelinca Dorin Jan 04 '12 at 10:46
  • Woops I tell a lie. Put the Sprite require in Sprite itself by mistake. Ok so new issue... same line - calling new Sprite() the error now is "object is not a function". Code is exactly as you have written it now. – Chris Evans Jan 04 '12 at 10:47
  • That works perfectly. Is it because I didn't assign the functions to a variable then? Would you mind explaining what difference that makes? Cheers! :) – Chris Evans Jan 04 '12 at 10:57
  • It doesn't make any difference using node ( becouse of it's modules, allso see that the ship is not assigned to a var ). However in some browsers depending on circumstances the function might end up in the global namespace ( window ), and i hate it so using `var` i make shure the function stays in the context it was defined and doesn't end up in the global namespace ( it's just a good practice ) – Poelinca Dorin Jan 04 '12 at 11:04
1

The problem is you didn't include module.exports = Sprite; at the end of the Sprite.js file. Note that I wrote exports and not export, so that typo must have been the problem.

alessioalex
  • 62,577
  • 16
  • 155
  • 122
  • That was a typo on here only, tried it as written in your post with no luck. :( "Sprite is not defined" - line 5 - (the = new Sprite() bit). – Chris Evans Jan 04 '12 at 10:37
  • Are you sure the server and the sprite files are on the same level? So make sure the Sprite.js file isn't inside some other folder by mistake. – alessioalex Jan 04 '12 at 10:40
  • Yup they're definately all there. See below. It's working to an extent because it's including the ship file ok. They work individually just not combined as above. – Chris Evans Jan 04 '12 at 10:44
  • Ah your problem is another: `Ship.prototype = new Sprite();` inside the Ship.js file. You are creating a new Sprite object without requiring the Sprite module there. – alessioalex Jan 04 '12 at 10:50