2012-08-10 67 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; 
}