2012-01-19 3 views
0

노드에 외부 jQuery 파일을 사용하는 데 문제가 있습니다. 내가 실행할 필요가 있지만 작동하지 않는 몇 가지 기능이 있습니다. 내가 테스트 할 수있는 간단한 함수를 만들었지 만 계속 오류 메시지가 발생했습니다 TypeError : Object # 'alert_helloworld'메서드가 없습니다. 여기 내 두 파일은 다음과 같습니다외부 jQuery 파일이있는 Node.js

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는 !!!! http://nodejs.org/docs/latest/api/modules.html

답변

1

당신은 수출이 templateparser.js에서 객체를 사용할 필요가있다. 기본적으로 node.js 모듈은 자체 격리 모듈 범위에 래핑됩니다.

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

그것은 당신이 실제로 당신이 필요로하는 기능을 내보내 중요 :

exports = exports || {}; 

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

은 모듈 문서에서 살펴 보자 :

+0

외부 파일을 재발견하고 싶지 않습니다. 나는 그것을 똑같이 지키고 싶다. 이것은 가능한가? – user1146581

+0

@ user1146581 예/아니요. jsdom에 자바 스크립트 파일로 삽입하거나 노드의 AST 파서를 통해 삽입 할 수 있습니다. 그렇지 않은 경우에는 파일에서 정의한 "전역"변수에 액세스 할 수 없습니다. – Raynos

+0

그래서 내가 물어 보는 것 같아, 어떻게 jsdom에 자바 스크립트로 주입합니까? – user1146581

관련 문제