Node.js
关于module.exports和exports区别
说白了,exports就是module.exports的快捷方式。
const exports = module.exports;
在脚本modA中写
exports.test = 100;
在脚本modB中尝试输出
const mod = require('./ modA.js');
console.log(mod.test);
可以看到输出:
C:\ Program Files \ nodejs \ node.exe --inspect-brk = 42773 modB.js
100
但是不能改变 exports的指向
继续在 modA中写
// exports.test = 100;
exports = {
a:1,
b:3,
test:100,
},
此时 modB输出:
undefined
现在的 exports是一个自变量对象,和module.export没什么关系了
我们可以这样写
module.exports = {
a:1,
b:3,
test:100,
};
输出就是 100 了