Javier Brannigan
Javier Brannigan

Reputation: 21

Object not defined into a module, Nodejs javascript

I'm developing a multiplayer game with Nodejs which will have a Lobby where I'll have all the logic related to hosting and connecting.

Actually I have three files server.js, lobby.js and game.j, the last one is where I want to do the logic of the game, which will run on the server.

When I initialite the server I require both lobby and game.

var Lobby = require("./server/lobby");
var Game= require("./server/game");

And after on creating a room I use a method inside the Lobby object.

socket.on('createRoom',function(roomName){
    Lobby.createRoom(roomName);
});

But into the Lobby when I create a new object game

createRoom: function(name){
    var game = new Game();

It tells me that Game is not defined and shut downs the server... What do I have to do to use the module Game inside Lobby??? it's my first time using nodejs and I'm still a newbie and I don't know how to make it work. :/

Thanks 4 your help!

Upvotes: 1

Views: 1672

Answers (2)

Patrick Roberts
Patrick Roberts

Reputation: 51876

var does not make an object global. In your lobby.js file, add

var Game = require('./game');

to the top of your code.

Upvotes: 1

Quentin
Quentin

Reputation: 943556

You have called it into the scope of the Server module, and can currently only access it from that scope.

You have to require the Game module inside the Lobby module in order to use it there.

Upvotes: 2

Related Questions