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
Export
Spritein FILE: Sprite.js like this :Then inside FILE: Ship.js ( this is the tricky part you’re missing ) use
requireto require the Sprite like this: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 :
File ship.js :
File main.js :
Run the example using
node main.jsand you should see :