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.
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.
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/
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):
Then you call in another file:
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:
Then put the following content into another file and execute it:
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:
Then you should use it like this:
In the following case, your module is an array:
Then you should use it like this:
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:
The following does the same thing:
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 objectmust be
when calling externally.exports.methodName = xxx();
when exporting, andWhen exported using
Module.exports
, an object{methodName: xxx()}
is exported, and when called externallyFor 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