t1.js
module.exports = function(app) {
return new obj(app);
};
var obj = function(app) {
this.app = app;
};
obj.prototype.query = function() {
return "ok";
};
t2.js
var a = require('./t1.js');
console.log(a.query());
执行节点t2.js
报告
console.log(a.query());
^
TypeError: Object function (app) {
return new obj(app);
} has no method 'query'
欢迎评论
答案 0 :(得分:1)
问题是require('./t1.js')
会返回您导出的函数。
以下是解决方案:
var a = require('./t1.js')();
/* or...
var myApp = {};
var a = require('./t1.js')(myApp);
*/
console.log(a.query()); //now it's "ok"