module
A reference to the current module, see the section about the module object. In particular,
module.exportsis used for defining what a module exports and makes avaiable throughrequire();exports
A reference to the module.exports that is shorter to type. See the section about the exports shortcut for details on when use
exportsand when to usemodule.exports.—— Node.js 官网
在Node.js中每个文件被视为一个module,每个module均是 Module 类实例化产生的。
通过官网可得知,直接使用module时,module对象是当前模块的引用,module.exports则是被用来定义一个文件要导出哪些可通过require()获得的内容。
直接使用exports时,它是一个在当前模块范围内可获得的变量,并且在此模块被执行前将module.exports的值分配给了它。当module.exports被一个新对象完全替换时,也会同样重新分配给exports,因此可以将exports看成module.exports的快捷方式。请记住,exports是一个变量,若被手动赋值则会丢弃module.exports的引用并且不会导出该值。
模块在第一次加载后便会被缓存进require.cache对象中,(根据其解析的文件名(包含路径)进行缓存,大小写敏感),除非更改require.cache,否则后续其他地方或每次调用require(modulename)将始终返回完全相同的对象。