This article will introduce the differences between exports and module. exports in Node. js. For more information, see this article.
In Node. js, what is the difference between exports and module. exports?
You must be familiar with the exports object used to create functions in the Node. js module (assuming a file named rocker. js ):
The Code is as follows: |
Copy code |
Exports. name = function (){ Console. log ('My name is lew.kilmister '); };
|
Then you call the following in another file:
The Code is as follows: |
Copy code |
Var rocker = require ('./rocker. js '); Rocker. name (); // 'My name is leader Kilmister'
|
But what exactly is module. exports? Is it legal?
Surprisingly,-module. exports is a real thing. Exports is only an auxiliary method for module. exports. Your module returns module. exports to the caller instead of exports. What exports does is collect attributes. If module. exports does not have any attributes, exports will assign these attributes to module. exports. If module. exports already has some attributes, all the items used in exports will be ignored.
Put the following content in rocker. js:
The Code is as follows: |
Copy code |
Module. exports = 'Rock IT! '; Exports. name = function (){ Console. log ('My name is lew.kilmister '); };
|
Put the following content in another file and execute it:
The Code is as follows: |
Copy code |
Var rocker = require ('./rocker. js '); Rocker. name (); // TypeError: Object rock it! Has no method 'name'
|
The rocker module ignores exports. name and returns a string 'Rock IT! '. Through the above example, you may realize that your module does not have to be a module instance ). Your module can be any legal JavaScript Object-boolean, number, date, JSON, string, function, array, and others. Your module can be any value you assign to module. exports. If you do not explicitly set any value for module. exports, the attribute in exports will be assigned to module. exports and then return it.
In the following cases, your module is a class:
The Code is as follows: |
Copy code |
Module. exports = function (name, age ){ This. name = name; This. age = age; This. about = function (){ Console. log (this. name + 'is + this. age + 'ears old '); }; };
|
Then you should use it like this:
The Code is as follows: |
Copy code |
Var Rocker = require ('./rocker. js '); Var r = new Rocker ('oss ', 62 ); R. about (); // oearis 62 years old
|
In the following cases, your module is an array:
The Code is as follows: |
Copy code |
Module. exports = ['lew.kilmister', 'oss osbourne', 'Ronnie James Dio ', 'steven count', 'Mick jarger'];
|
Then you should use it like this:
The Code is as follows: |
Copy code |
Var rocker = require ('./rocker. js '); Console. log ('rockin heaven: '+ rocker [2]); // Rockin heaven: Ronnie James Dio
|
Now you should find the key points-if you want your module to become a special object type, use module. exports; if you want your module to become a traditional module instance, use exports.
The result of assigning the attribute to module. exports is the same as assigning the attribute to exports. Let's look at the example below:
The Code is as follows: |
Copy code |
Module. exports. name = function (){ Console. log ('My name is lew.kilmister '); };
|
The following is the same thing:
The Code is as follows: |
Copy code |
Exports. name = function (){ Console. log ('My name is lew.kilmister '); };
|
But please note that they are not the same thing. As I mentioned earlier, module. exports is a real thing, and exports is only an auxiliary method. Even so, exports is a recommended object unless you want to change the object type of your module from the traditional module instance to another one.
I hope this article will help you understand the differences between exports and module. exports, and further understand how modules work in Node. js. If you have any questions, please leave a message in the reply.