Javascript Express.JS无法识别所需的JS文件';s函数

Javascript Express.JS无法识别所需的JS文件';s函数,javascript,express,Javascript,Express,尽管我已经导入了包含我将要使用的函数的JS文件,Node.JS说它是未定义的 require('./game_core.js'); Users/dasdasd/Developer/optionalassignment/games.js:28 thegame.gamecore = new game_core( thegame ); ^ ReferenceError: game_core is not defined 你知道怎么了吗?游戏核心

尽管我已经导入了包含我将要使用的函数的JS文件,Node.JS说它是未定义的

require('./game_core.js');

Users/dasdasd/Developer/optionalassignment/games.js:28
    thegame.gamecore = new game_core( thegame );
                       ^
ReferenceError: game_core is not defined
你知道怎么了吗?游戏核心包括以下功能:

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

添加到game_core.js的末尾:

module.exports = {  
    game_core : game_core  
}  
至games.js:

var game_core = require('./game_core').game_core(game_istance);

添加到game_core.js的末尾:

module.exports = {  
    game_core : game_core  
}  
至games.js:

var game_core = require('./game_core').game_core(game_istance);

需要节点中的模块不会将其内容添加到全局范围。每个模块都包含在自己的范围内,因此您必须:

然后在主脚本中保留对导出对象的引用:

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

您可以在文档中了解更多信息:

需要节点中的模块不会将其内容添加到全局范围。每个模块都包含在自己的范围内,因此您必须:

然后在主脚本中保留对导出对象的引用:

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

您可以在文档中了解更多信息:

另一种方法:

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

另一种办法:

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