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

Source: Internet
Author: User

Node.js introduces the concept of module (module), in which a module can export functions, variables, etc. through module.exports or exports so that other JavaScript scripts are introduced and used by the require () function.

The Module.exports initial value is an empty object {}, so the exports initial value is also {},exports is the reference to the module.exports that is pointing to, presumably within the module:

Exports = Module.exports = {};

To raise a chestnut, creating a module in Node.js is very simple, a file is a module, so we create a name.js file to create a module name.js, using exports and require object to provide external interface and reference module.

Name.js

var myname=function () {
var name= ' Amberylopez ';
Console.log (name);
Exports.myname=myname;

This is what you need to do when you use it.

App.js

var name=require ('./name ');

If we create a name.js file that uses Module.exports and require objects to provide interfaces and reference modules externally.

Name.js

var myname=function () {
var name= ' Amberylopez ';
Console.log (name);
Module.exports=myname;

App.js

var name=require ('./name ');

Exports assignment is actually to module.exports this empty object to add the MyName property just, why exports to use the way to add attributes, without Exports=myname?

Exports is a value that references module.exports. When the exports is changed, the module.exports will not be changed, and when the module is exported, the actual export execution is module.exports, not exports.

If you change the name.js to

var myname=function () {
var name= ' Amberylopez ';
Console.log (name);
Exports=myname;

App.js

var name=require ('./name '); <br>console.log (name);

The operation will be an error. Because, earlier, by adding attributes to the exports, and now modifying the memory pointed to by the exports, exports and module.exports no longer point to the same memory, that is, the memory that the module.exports points to does not make any changes, is still an empty object {}, so an error is not available.

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.