![](/img/trans.png)
[英]TypeError: fn is not a function. Difference between two functions in module.exports
[英]Is module.exports = { fn } same as exports.fn = fn
对于作业测试,我需要创建这样的库:
// do-it.js
function doIt(smth) {}
module.exports = {
doIt,
};
但我正在使用 typescript 和编译的 do-it.ts 文件如下所示:
// do-it.js when compiled from do-it.ts
exports.__esModule = true;
exports.doIt= exports.another = void 0;
function doIt(smth) {}
exports.doIt = doIt;
function another() {}
exports.another= another;
这两个示例的导出是否相同?
粗略地说,是的; 当然,由此产生的出口是相同的。
我的意思是,如果你这样做了:
module.exports = {
doIt,
};
后来做了
module.exports = {
doSomethingElse,
};
你会遇到问题,因为第二个完全替换了之前的exports
object。
您根本不需要创建 object,它是在调用模块之前为您创建的。 所以真的,你可以做
exports.doIt = doIt;
接着
exports.doSomethingElse = doSomethingElse;
首先。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.