node.js - exports和module.exports的区别
迷茫
迷茫 2017-04-17 11:20:47
0
6
573

在node.js里经常看到两者混用,如果两者意义相同为什么看到有的项目里既有exports又有module.exports

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(6)
Peter_Zhu

Sigh, if you don’t know Google, it’s hard to become a qualified programmer... Just search with your title, and the titles of the top 10 results will all match

Pick 3 for you

The difference between exports and module.exports in Node.js

Node.js series - module.exports and exports

NodeJS study notes-the difference between exports and module.exports

伊谢尔伦

Original text: http://www.hacksparrow.com/node-js-exports-vs-module-exports.html

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

You must be familiar with the exports object used to create functions in Node.js modules (assuming a file named rocker.js):

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

Then you call in another file:

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

But what exactly is module.exports? Is it legal?

Surprisingly - module.exports is a real thing. exports is just a helper method of module.exports. Your module ultimately returns module.exports to the caller, not exports. What exports does is collect attributes. If module.exports currently does not have any attributes, exports will assign these attributes to module.exports. If module.exports already has some properties, everything used in exports will be ignored.

Put the following content into rocker.js:

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

Then put the following content into another file and execute it:

var rocker = require('./rocker.js');
rocker.name(); // TypeError: Object ROCK IT! has no method 'name'

The rocker module completely ignores exports.name and returns a string 'ROCK IT!'. From the above example, you may realize that your modules do not necessarily have to be module instances. Your module can be any legal JavaScript object - boolean, number, date, JSON, string, function, array and others. Your module can be any value you give module.exports. If you do not explicitly set any value for module.exports, the properties in exports will be assigned to module.exports and returned.

In the following case, your module is a class:

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

Then you should use it like this:

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

In the following case, your module is an array:

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

Then you should use it like this:

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

Now you should get the gist - if you want your module to be a specific object type, use module.exports; if you want your module to be a traditional module instance, use exports.

The result of assigning attributes to module.exports is the same as assigning attributes to exports. Look at this example:

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

The following does the same thing:

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

But please note that they are not the same thing. Like I said before, module.exports is a real thing, exports is just its auxiliary method. Having said that, exports are still the recommended object unless you want to change the object type of your module from a traditional module instance to something else.

迷茫

To put it simply, exports refers to modules.exports
So
exports.fn = function(){};, modules.exports.fn also points to the anonymous function

Also, something worth noting
After executing exports = null, modules.exports still points to the original value, not equal to null

阿神

There are pictures and the truth, pictures and texts reveal the usage of exports and module.exports:
http://qifuguang.me/2015/11/11/%E6%8F%AD%E7%A7%98Node-js %E4%B8%ADexports%E5%92%8Cmodule-exports/

小葫芦

Can it be simply understood as:
The name of the module mentioned below is the file name. If the file name is abc.js, this module is called abc module.

exports is to export a certain method to the module object named after the file name, that is, to import a certain method into the module object abc.
Module.exports What is exported is actually the entire module object

must be exports.methodName = xxx(); when exporting, and

when calling externally.
var abc = require("abc.js");
abc.methodName();

When exported using Module.exports, an object {methodName: xxx()} is exported, and when called externally

var abc = require("abc.js");
abc.methodName();

For details, please see
http://sabrinaluo.com/tech/2015/12/16/difference-between-Module-exports-with-exports-in-NodeJS/

迷茫

... Read the manual, give it a try, and you will understand everything

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template