1

Even though, I've imported the JS file that includes the function that I will use, Node.JS says that it is undefined.

require('./game_core.js');

Users/dasdasd/Developer/optionalassignment/games.js:28
    thegame.gamecore = new game_core( thegame );
                       ^
ReferenceError: game_core is not defined

Do you have any idea what's wrong? Game_core includes the function:

var game_core = function(game_instance){....};

3 Answers 3

4

Add to the end of game_core.js:

module.exports = {  
    game_core : game_core  
}  

to games.js:

var game_core = require('./game_core').game_core(game_istance);
Sign up to request clarification or add additional context in comments.

Comments

2

Requiring a module in Node doesn't add its contents to the global scope. Every module is wrapped in its own scope, so you have to export public names:

// game_core.js
module.exports = function (game_instance){...};

Then keep a reference in your main script to the exported object:

var game_core = require('./game_core.js');
...
thegame.gamecore = new game_core( thegame );

You can read more about it in the docs: http://nodejs.org/api/modules.html#modules_modules

Comments

0

An alternative approach:

if( 'undefined' != typeof global ) {
    module.exports = global.game_core = game_core;
}

Comments

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.