First, let’s take a look at the official documentation
module.exports
A reference to the current module, see the section about the module
object. In particular, module.exports
is used for defining what a module exports and makes available through require()
.
The module object is a reference to the current module object. Module. exports is created by the Module system to represent the content to be exported from that module
exports
-
A reference to the
module.exports
that is shorter to type. -
The
exports
variable is available within a module’s file-level scope, and is assigned the value ofmodule.exports
before the module is evaluated. -
It allows a shortcut, so that
module.exports.f = ...
can be written more succinctly asexports.f = ...
. However, be aware that like any variable, if a new value is assigned toexports
, it is no longer bound tomodule.exports
:module.exports.hello = true; // Exported from require of module exports = { hello: false }; // Not exported, only available in the module Copy the code
Exports is a shorthand for module.exports, which is scoped at the file level, just like module scoped.
Exports and module.exports refer to the same reference by default (starting with {}), that is, exports is a reference to module.exports
console.log(exports, module.exports, exports === module.exports);
// {} {} true
Copy the code
. That is modify the exports or the module exports any value will lead to another value of synchronization, which means you can use any way to export the contents of the module, the require (module) of content is {2} a:
module.exports.a = 2;
console.log(exports, module.exports, exports === module.exports);
// {a: 2} {a: 2} true
exports.a = 2;
console.log(exports, module.exports, exports === module.exports);
// {a: 2} {a: 2} true
Copy the code
At this moment, the fine line
If you modify module.exports and exports at the same time, what does require(module) import?
exports.a = 2;
module.exports.a = 3;
console.log(exports, module.exports, exports === module.exports);
// {a: 3} {a: 3} true
Copy the code
-
Exports === module.exports = true
-
Because the value changed at this point only changes the base datatype, not the reference to the value, the values of these two people are unchanged
-
After modifying the reference to the value at the same time, let’s see what happens after require(module).
-
exports.a = 2; module.exports = { b: 3 }; console.log(exports, module.exports, exports === module.exports); {a: 2} {b:3}} {b:3}Copy the code
Description: Whether exported from exports or module.exports, the module content accepted by Requeire is exported from the Module. exports object.
Then there is the way they export a little bit differently. Exports only support use. The syntax exposes internal variables, which is supported by module.export. The syntax supports copying an object directly
It’s quite a mess, so let’s sum it up:
With:
-
Both can expose modules outwards
-
References to both values point to the same place
Vision:
-
Require only accepts variables exposed by module.exports
-
Exports only supports. Syntax, Module. exports supports. Syntax and direct assignment object forms
Shout ~ finished, happy heart