Node.js Module – exports vs module.exports

来源:互联网 发布:csv导入oracle数据库 编辑:程序博客网 时间:2024/05/22 15:45

文章:https://www.hacksparrow.com/node-js-exports-vs-module-exports.html

exports:是node.js的全局对象

What is the difference between exports and module.exports in Node.js?

一、   在rocker.js写下面内容:

exports.name = function() {    console.log('My name is Lemmy Kilmister');};
按照javascript语法,就是 给exports对象添加方法name();

 在另一个文文件写以下代码,变量rocker指向rocker.js文件,同时表示有name()这个函数,

  下面的意思是不是说,只要在一个js里,用exports产生的方法,用以下语句全部可以直接调用方法???

var rocker = require('./rocker.js');rocker.name(); // 'My name is Lemmy Kilmister'

二、rocker.js写下面内容,第一行,表示exports是module对象的属性.

module.exports = 'ROCK IT!';exports.name = function() {    console.log('My name is Lemmy Kilmister');};

   在另一个js里,调用rock.js,这时直接调用方法出错,name()方法应该属于module.exports.name()的方法

var rocker = require('./rocker.js');rocker.name(); // TypeError: Object ROCK IT! has no method 'name'
 上面调用出错,因为module是一个类, 

module.exports = function(name, age) {    this.name = name;    this.age = age;    this.about = function() {        console.log(this.name +' is '+ this.age +' years old');    };};

   必须以构造函数形势调用模板

var Rocker = require('./rocker.js');var r = new Rocker('Ozzy', 62);r.about(); // Ozzy is 62 years old

In this case, your module is an array:

module.exports = ['Lemmy Kilmister', 'Ozzy Osbourne', 'Ronnie James Dio', 'Steven Tyler', 'Mick Jagger'];

and you may use it this way:

var rocker = require('./rocker.js');console.log('Rockin in heaven: ' + rocker[2]); //Rockin in heaven: Ronnie James Dio

So you get the point now - if you want your module to be of a specific object type, use module.exports; if you want your module to be a typical module instance, use exports.

The result of attaching properties to module.exports is akin to attaching properties to exports. For example this:

module.exports.name = function() {    console.log('My name is Lemmy Kilmister');};

does the same thing as:

exports.name = function() {    console.log('My name is Lemmy Kilmister');};

But note that, they are not the same thing. As I said earlier module.exports is the real deal, exports is just its little helper. Having said that, exports is the recommended object unless you are planning to change the object type of your module from the traditional 'module instance' to something else.

I hope this post helped you understand the difference between exports and module.exports, and learn a bit more about how modules work in Node.js. Any questions, ping me in the comments.

UPDATE: 7th Feb, 2014

As long are you don't overwrite the module.exports object with an assignment operation, anything attached to module.exports and exports will be available in the 'required' module.

If this is the content of your module:

module.exports.age = 68;exports.name = 'Lemmy Kilmister';

The following code would work fine:

var rocker = require('./rocker.js');console.log('%s is %s', rocker.name, rocker.age); // Lemmy Kilmister is 68

BUT

if you overwrite module.exports with anything in your module, it will fail:

module.exports = 'LOL';module.exports.age = 68;exports.name = 'Lemmy Kilmister';

or

module.exports.age = 68;exports.name = 'Lemmy Kilmister';module.exports = 'WTF';

the order doesn't matter, rocker.age and rocker.name will now be undefined.

Also, note: just because module.exports.age and exports.name are exported, does not mean you should use a combination of both. My recommendation is to stick to exports.*, and be aware of module.exports.*.