在 Node.js 中,“exports”和“module.exports”两者的区别有以下几点:
导出对象类型不同: exports 是对 module.exports 的一个全局引用,而实际导出的是 module.exports 对象。即 exports 只是为了方便,可以在不断开 module.exports 的情况下,挂载属性和方法到 exports 上,最终再将其赋值给 module.exports,从而达到返回多个 API 的目的。
直接赋值的效果不同:当直接对 exports 赋值时,它不会覆盖 module.exports 的引用,因此不能使用 exports 代替 module.exports 赋值(i.e., 模块导出的内容)。