我试图遵循模块的基本指南。我创建了 test_module.js
var textFunction = function() {
console.log("text");
};
exports = textFunction;
然后我尝试在我的 app.js 中使用它:
var textFunction = ('./test_module');
textFunction();
但我收到一个错误:
TypeError: textFunction is not a function
难道我做错了什么?或者这是一本非常古老的指南?
PS:只有当我这样声明时,导出才有效:
exports.text = function() {
console.log("text");
}
exports
是一个局部变量。分配给它不会改变导出的值。您想要分配给module.exports
直接地:
module.exports = textFunction;
module.exports
and exports
最初引用相同的值(一个对象),但分配给exports
不会改变module.exports
,这才是最重要的。exports
存在是为了方便。
另一个问题是您没有正确需要该模块,但这可能只是一个拼写错误。你应该做
var textFunction = require('./test_module');
var textFunction = ('./test_module');
只是分配字符串'./test_module'
to textFunction
我们都知道字符串不是函数。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)