2012-01-19 69 views
0

我有一個問題,利用外部jQuery文件與節點。我有幾個功能,我需要運行,但它不工作。我創建了一個簡單的函數來測試,但是我收到的錯誤消息是TypeError:Object#沒有方法'alert_helloworld'。這裏是我的兩個文件:Node.js與外部jQuery文件

example.js

var jsdom = require('jsdom'); 
var templateparser = require('./templateparser.js'); 
var test = templateparser.alert_helloworld(); 

templateparser.js

function alert_helloworld(){ 
console.log("Hello World"); 
return false; 
} 

HELP !!!!

回答

0
module.exports.alert_helloworld = function alert_helloworld(){ 
    console.log("Hello World"); 
    return false; 
}; 

它你實際上導出你需要的功能是非常重要的。默認情況下,node.js模塊被封裝在它們自己的隔離模塊範圍內。

+0

我不想重塑我的外部文件。我想保持它一樣。這甚至有可能嗎? – user1146581

+0

@ user1146581是/否。您可以將它作爲JavaScript文件注入到jsdom中,或者您可以通過節點中的AST解析器進行注入。否則,不能訪問您在文件中定義的「全局」變量。 – Raynos

+0

所以我想我問,我如何將它作爲JavaScript注入jsdom? – user1146581