2012-01-19 66 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