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

Source: Internet
Author: User
Tags dio

Original: http://www.hacksparrow.com/node-js-exports-vs-module-exports.html

You must Node.js be familiar with the object used to create the function in the module exports (assuming a rocker.js file named):

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 module.exports what the hell is this thing?

Amazingly- module.exports there is something real. exportsjust module.exports the helper method. Your module eventually returns module.exports to the caller instead of exports . The exports thing to do is to collect the attributes, and if there are module.exports no attributes at the moment, exports assign them module.exports . If module.exports there are already some attributes, then the exports things used in it will be ignored.

Put the following 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, execute it:

var rocker = require ('./rocker.js '); Rocker.name (); Typeerror:object ROCK It! Has no method ' name '

  

The rocker module was completely ignored and exports.name then returned a string of ' ROCK it! '. From the example above, you may realize that your module does not necessarily have to be a module instances. Your module can be any legitimate JavaScript object-Boolean,number,date,json, String,function,array and others. Your module can be any value that you give module.exports . If you do not explicitly module.exports set any value, then exports the property in is assigned to module.exports it, and then it is returned.

In the following case, your module is a class:

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 like this:

var = require ('./rocker.js '), var r = new Rocker (' Ozzy ', rocker); R.about (); Ozzy is years old

In the following case, your module is an array:

Module.exports = [' Lemmy Kilmister ', ' Ozzy Osbourne ', ' Ronnie James Dio ', ' Steven Tyler ', ' Mick Jagger '];

Then you should use it like this:

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 it module.exports ; If you want your module to be a traditional 模块实例 (module instance), use it exports .

The attribute module.exports is given the same result as the attribute exports . Look at the following example:

Module.exports.name = function () {    console.log (' My name is Lemmy Kilmister ');};

The following is the same thing to do:

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 , there is something real, exports but it's an auxiliary method. That being said, it exports is a recommended object unless you want to modify the object type of your module from the traditional 模块实例 (module instance) to the other.

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.