ES6标准发布后,module成为标准,标准的使用是以export指令导出接口,以import引入模块,但是在我们一贯的node模块中,我们采用的是CommonJS规范,使用require引入模块,使用module.exports导出接口。
Node内部提供一个Module构建函数。所有模块都是Module的实例。每个模块内部,都有一个module对象,代表当前模块。它有以下属性。
module.exports属性表示当前模块对外输出的接口,其他文件加载该模块,实际上就是读取module.exports变量。 使用module.exports可以将文件中的某个模块(函数或是变量)暴露出去让外部调用
//example.js
var x = 5;
var fn = function (value) {
return value + x;
};
global.warning = true;
module.exports = fn;
//index.js
var mod = require('./example.js');
// global
console.log(warning); //interesting
console.log(global.warning); //interesting
console.log(global.x); //undefined
// module.exports
console.log(mod(10)); //15
Node为每个模块提供一个exports变量,指向module.exports。 相当于var exports = module.exports;
//index.js
var exp=require('./exports');
console.log(exp.area(5)); //78.53981633974483
//export.js
exports.area = function (r) {
return Math.PI * r * r;
};
//index.js
console.log(mod.area); //undefined
console.log(mod(10)); //15
//example.js
var x = 5;
var fn = function (value) {
return value + x;
};
module.exports = fn;
exports.area = function (r) {
return Math.PI * r * r;
};
由以上运行结果可得出结论:
//example.js
exports = {name:Tom};
exports.area = function (r) {
return Math.PI * r * r;
};
//index.js
var mod = require('./example.js');
console.log(mod); //{}
只使用exports时,先给exports添加属性或方法再给exports赋值会导致赋值无法存储
//example.js
exports.area = function (r) {
return Math.PI * r * r;
};
exports = {name:Tom};
//index.js
var mod = require('./example.js');
console.log(mod); //{ area: [Function] }
console.log(mod.area(10)); //314.1592653589793
通俗点讲,exports先赋值为某个固定值,再添加属性会导致引用结果为空;exports先添加属性,再赋值为某个固定值结果为添加的属性对象。
使用建议 不要混用exports与module.exports 如果你觉得,exports与module.exports之间的区别很难分清,一个简单的处理方法,就是放弃使用exports,只使用module.exports。