在node中,每个js文件都是一个模块, 在模块中有一个module对象, module对象就表示当前模块自身;
在module对象中有一个exports属性, 可以通过exports属性暴露数据;默认值是一个空对象{}
在nodejs中,模块设置导出项(暴露给外界使用的数据),一般用module.exports 这个属性

module.exports.age = age;
module.exports.say = function () {
  console.log('say');  
}

但是exports也能用

exports.age = age;
exports.say = function () { 
  console.log('say'); 
}

这两者之间有什么区别呢,经过一番谷歌百度,发现exports只是module.exports的一个引用
说以说不能给它直接赋值,直接赋值 就不指向module.exports了 例如

module.exports = {
  name: 'zs',
  age: 20
};

exports = {
  sex: '男',
  height: 170
}

这样一来module.exports会生效而exports不会生效

Last modification:August 13, 2019
If you think my article is useful to you, please feel free to appreciate