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

Source: Internet
Author: User
Tags dio

Think your module is a specific type just use Module.exports. If the module you want is a typical "instanced object", use exports.

You must be very familiar with the exports object in the Nodejs module, which you can use to create your module. For example: (assuming this is a rocker.js file)

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


In another file, you refer to this

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


What the hell is module.exports? Is it legal?
In fact, Module.exports is the real interface, exports is just one of its auxiliary tools. The final return to the call is module.exports instead of exports.

All the properties and methods collected by the exports are assigned to Module.exports. Of course, there is a premise that the module.exports itself does not have any attributes or methods. If Module.exports already has some properties and methods, the information collected by exports will be ignored.

Modify the Rocker.js as follows:

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


Re-referencing execution rocker.js

Copy CodeThe code is as follows:
var rocker = require ('./rocker.js ');
Rocker.name (); Typeerror:object ROCK It! Has no method ' name '


Error found: Object "ROCK it!" No Name Method
The rocker module ignores the name method collected by exports and returns a string "ROCK it!". Your module does not necessarily have to return "instanced objects". Your module can be any legitimate JavaScript object--boolean, number, date, JSON, string, function, array, and so on.

Your module can be anything you set to it. If you do not explicitly set any properties and methods for Module.exports, then your module is the exports set to Module.exports property.

In the following example, your module is a class:

Copy CodeThe code is as follows:
Module.exports = function (name, age) {
THIS.name = name;
This.age = age;
This.about = function () {
Console.log (THIS.name + ' is ' + this.age + ' years-old ');
};
};


You can apply it this way:

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


In the following example, your module is an array:

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


You can apply it this way:

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


Now you understand that if you want your module to be a specific type just use Module.exports. If the module you want is a typical "instanced object", use exports.

Adding a property to Module.exports is similar to adding a property to exports. For example:

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


Again, exports is like this.

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


Note that both of these results do not want to be the same. As mentioned earlier, Module.exports is a true interface, and exports is nothing more than an auxiliary tool. It is recommended to export using exports unless you intend to change from the original "Instanced object" to a type.

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

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.