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

Source: Internet
Author: User
Tags dio

What is the difference between exports and module.exports in Node.js?
You must be familiar with the exports object used to create the function in the Node.js module (suppose a file named Rocker.js):

The code is as follows Copy Code
Exports.name = function () {
Console.log (' My name is Lemmy Kilmister ');
};

And then you call in another file:

The code is as follows Copy Code
var rocker = require ('./rocker.js ');
Rocker.name (); ' My name is Lemmy Kilmister '

But what the hell is module.exports? Is it legal?

Surprisingly,-module.exports is a real thing. Exports is only a module.exports auxiliary method. Your module eventually returns module.exports to the caller, not to the exports. What exports does is collect attributes, and if Module.exports doesn't currently have any attributes, exports will give module.exports these attributes. If Module.exports already has some properties, then the things used in exports will be ignored.

Put the following on the Rocker.js:

The code is as follows Copy Code
Module.exports = ' Rock it! ';
Exports.name = function () {
Console.log (' My name is Lemmy Kilmister ');
};

Then put the following into 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 completely ignores exports.name and then returns a string of ' rock it! '. From the example above, you may realize that your module does not necessarily have to be a module instance (modular instances). Your module can be any legitimate 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 to Module.exports, the attributes in exports are assigned to the Module.exports and then returned.

In the following case, 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 + ' years old ');
};
};

Then you should use it this way:

The code is as follows Copy Code
var rocker = require ('./rocker.js ');
var r = new Rocker (' Ozzy ', 62);
R.about (); Ozzy is years old

In the following case, your module is an array:

The code is as follows Copy Code
Module.exports = [' Lemmy Kilmister ', ' Ozzy Osbourne ', ' Ronnie James Dio ', ' Steven Tyler ', ' Mick Jagger '];

Then you should use it this way:

The code is as follows Copy Code
var rocker = require ('./rocker.js ');
Console.log (' Rockin in Heaven: ' + rocker[2]); Rockin in Heaven:ronnie James Dio

Now you should find the point-if you want your module to become a special object type, then use Module.exports; If you want your module to become a traditional module instance (modular instance), use exports.

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

The code is as follows Copy Code
Module.exports.name = function () {
Console.log (' My name is Lemmy Kilmister ');
};

The following is the same thing to do:

The code is as follows Copy Code
Exports.name = function () {
Console.log (' My name is Lemmy Kilmister ');
};

But please note that they are not the same thing. As I said before, Module.exports is a real thing, and exports is just its auxiliary method. That said, exports is a recommended object unless you want to modify the object type of your module from a traditional module instance (modular instance) to something else.

I hope this article will help you understand the difference between exports and module.exports and further understand how the module works 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.