function Module(id, parent) { this.id = id; this.exports = {}; this.parent = parent; if (parent && parent.children) { parent.children.push(this); } this.filename = null; this.loaded = false; this.children = []; }
index.js
html
console.log(module)
Module { id: '.', exports: {}, parent: null, filename: '/home/joes/Demo/tests/index.js', loaded: false, children: [], paths: [ '/home/joes/Demo/tests/node_modules', '/home/joes/Demo/node_modules', '/home/joes/node_modules', '/home/node_modules', '/node_modules' ] }
file1.js
node
module.exports = function fn() {};
index.js
函数
var file1 = require('./file1.js') console.log(module)
Module { id: '.', exports: {}, parent: null, filename: '/home/joes/Demo/tests/index.js', loaded: false, children: [ Module { id: '/home/joes/Demo/tests/file1.js', exports: [Function], parent: [Circular], filename: '/home/joes/Demo/tests/file1.js', loaded: true, children: [], paths: [Object] } ], paths: [ '/home/joes/Demo/tests/node_modules', '/home/joes/Demo/node_modules', '/home/joes/node_modules', '/home/node_modules', '/node_modules' ] }
咱们时常会在模块源码中看到ui
exports = module.exports = {};
此时两者指向同一个对象。若是export.a = 1;
此时this
exports = module.exports = {a: 1}
若是使用exports = function() {};
并无导出module,exports
指向了新的对象,module.exports仍是指向原始的对象。又由于module.exports
对象是要传给require
函数的,因此以后的改变并不会实际导出到外面。spa
http://www.hacksparrow.com/node-js-exports-vs-module-exports.html
http://stackoverflow.com/questions/7137397/module-exports-vs-exports-in-node-jscode