module.exports TypeError并没有方法

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' 

您的评论欢迎

Solutions Collecting From Web of "module.exports TypeError并没有方法"

问题是require('./t1.js')返回一个你导出的函数。

这是解决scheme:

 var a = require('./t1.js')(); /* or... var myApp = {}; var a = require('./t1.js')(myApp); */ console.log(a.query()); //now it's "ok"