The difference between exports and module.express

Each node.js executable file automatically creates a module object. At the same time, the module object creates a property called exports, and the initialized value is {}

 module.exports = {};

exports and module.exports point to the same memory, but require() returns module.exports instead of exports.

var str = "difference"
exports.a = str;
exports.b = function () {

}

Assigning a value to exports is actually adding two properties to the empty object module.exports. The above code is equivalent to:

var str = "difference"
module.exports.a = str;
module.exports.b = function () {

}

Let's take a look at the use of exports and module.exports

use exports

app.js

var s = require("./log");
s.log("hello");

log.js


exports.log =function (str) {
    console.log(str);
}

用module.exports

app.js

var s = require("./log");
s.log("hello");

log.js

module.exports =function (str) {
    console.log(str);
}

Both of the above usages are fine, but if used like this

  exports = function (str) {
    console.log(str);
}

Running the program will report an error.

In the previous example, adding attributes to exports only modifies the memory pointed to by exports. In the above example, the memory pointed to by exports is overwritten, so that exports point to a new memory. In this way, the memory pointed to by exports and module.exports is not the same block, and exports and module.exports have nothing to do with each other. The memory pointed to by exports has changed, but the memory pointed to by module.exports has not changed, and is still an empty object {}.

require will get an empty object, there will be

TypeError: s.log is not a function

 Error message.

Look at the example below

app.js

 var x = require('./init');

 console.log(x.a)

init.js

 module.exports = {a: 2}
 exports.a = 1 

Running app.js will output

2

This means that when the module.exports object is not empty, the exports object is automatically ignored, because module.exports is already different from the variable pointed to by the exports object through the assignment method, and how the exports object is changed has nothing to do with the module.exports object.

 exports = module.exports = somethings

Equivalent to

  module.exports = somethings
  exports = module.exports

The reason is also very simple. module.exports = somethings is to overwrite module.exports. At this time, the relationship between module.exports and exports is broken. Module.exports points to a new memory block, while exports still points to the original memory block. In order to Let module.exports and exports still point to the same piece of memory or the same "object", so we export = module.exports.

Finally, the relationship between exports and module.exports can be summarized as

  1. The initial value of module.exports is an empty object {}, so the initial value of exports is also {}
  2. exports is a reference to module.exports
  3. require() returns module.exports instead of exports

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325222128&siteId=291194637