Differences between exports and module. exports in Node. js

Source: Internet
Author: User
Tags dio

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.