Explain the difference between exports and module. exports in nodejs in detail

  • 2021-07-21 07:21:27
  • OfStack

require is used to load the code, while exports and module. exports are used to export the code. However, many novices may be puzzled by the difference between exports and module. exports. In order to better understand the relationship between exports and module. exports, let's first consolidate the foundation of js. Example:

app.js


var a = {name: 'nswbmw 1'};
var b = a;
console.log(a);
console.log(b);
b.name = 'nswbmw 2';
console.log(a);
console.log(b);
var b = {name: 'nswbmw 3'};
console.log(a);
console.log(b);

Running app. js results:


{ name: 'nswbmw 1' }
{ name: 'nswbmw 1' }
{ name: 'nswbmw 2' }
{ name: 'nswbmw 2' }
{ name: 'nswbmw 2' }
{ name: 'nswbmw 3' }

Explanation 1: a is an object, b is a reference to a, that is, a and b point to the same object, that is, a and b point to the same memory address, so the first two outputs one sample. When b is modified, that is, the contents of a and b pointing to the same memory address have changed, so a will also be reflected, so the third. 4 outputs one sample. When b is completely overwritten, b points to a new memory address (without modifying the original memory block), and a still points to the original memory block, that is, a and b no longer point to the same memory block, that is to say, a and b have nothing to do at this time, so the last two outputs are different.

After understanding the above examples, let's get down to business.

We only need to know three things to know the difference between exports and module. exports:

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

So: We pass


var name = 'nswbmw';
exports.name = name;
exports.sayName = function() {
 console.log(name);
}

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


var name = 'nswbmw';
module.exports.name = name;
module.exports.sayName = function() {
 console.log(name);
}

We usually use exports and module. exports like this

A simple example to calculate the area of a circle:

Using exports

app.js


var circle = require('./circle');
console.log(circle.area(4));

circle.js


exports.area = function(r) {
 return r * r * Math.PI;
}

Use module. exports

app.js


var area = require('./area');
console.log(area(4));

area.js


module.exports = function(r) {
 return r * r * Math.PI;
}

The output of the above two examples is 1. You may ask, why not write like this?

app.js


var area = require('./area');
console.log(area(4));

area.js


exports = function(r) {
 return r * r * Math.PI;
}

Running the above example will report an error. This is because in the previous example, by adding attributes to exports, only the memory pointed to by exports was modified, and


{ name: 'nswbmw 1' }
{ name: 'nswbmw 1' }
{ name: 'nswbmw 2' }
{ name: 'nswbmw 2' }
{ name: 'nswbmw 2' }
{ name: 'nswbmw 3' }
0

In fact, it covers exports. That is to say, exports points to a new block of memory (the content is a function to calculate the area of a circle), that is to say, exports and module. exports no longer point to the same block of memory, that is to say, exports and module. exports have no connection at this time, that is to say, the block of memory pointed by module. exports has not been changed, and it is still an empty object {}, that is to say, area. js exports an empty object, so we call area (4) in app. js and report TypeError:

So, to sum up in one sentence: exports and module. exports can be used when we want the module to export an object (but exports cannot be overwritten as a new object), while module. exports must and can only be overwritten when we want to export a non-object interface.

We often see this writing:

exports = module.exports = somethings;

The above code is equivalent to


{ name: 'nswbmw 1' }
{ name: 'nswbmw 1' }
{ name: 'nswbmw 2' }
{ name: 'nswbmw 2' }
{ name: 'nswbmw 2' }
{ name: 'nswbmw 3' }
1

The reason is also very simple. module. exports = somethings overwrites 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 make module. exports and exports still point to the same memory or point to the same "object", we will use exports = module. exports.


Related articles: