2012-08-10 65 views
1

尽管我导入了包含我将使用的函数的JS文件,但Node.JS表示它未定义。Express.JS无法识别所需的js文件的功能

require('./game_core.js'); 

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

你知道有什么问题吗? Game_core包括功能:

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

回答

4

添加到game_core.js结束:

module.exports = { 
    game_core : game_core 
} 

到games.js:

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

要求在节点模块不添加其内容到全球范围。每个模块都被包裹在自己的范围内,所以你必须export public names

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

然后在你的主脚本保持一个参考导出的对象:

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

你可以在阅读更多关于它文档:http://nodejs.org/api/modules.html#modules_modules

0

的另一种方法:

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