Nodejs compile JS file to the JS file packaging
Nodejs Compile the JS file will be the content of the JS file to the end of the packaging (function (exports,require,module,__filename,__dirname) {\ n and \}), the normal JS file becomes:
(function (exports,require,module,__filename,__dirname) {
var a=function () {};
..............
})
After packing, we will have variable exports,require,module in every JS file. The packaging prevents global contamination
What is the difference between exports and module.exports?
After wrapping the contents of the JS file, a function is returned, and in the arguments passed when the function executes, exports and module.exports point to the same variable that can be verified in the following way:
Save the following code to the Nodejsbuild.js file
Console.log (arguments)
Console.log (Exports==exports)
Run Node Nodejsbuild.js
The exports and model.exports that are passed in initially are equal when we assign operations to exports or Models.exports to assign operations, the relationship between the two is destroyed, and the last use is actually model.exports. When I used it, I found that there was a problem.
Through the above if you can clearly know what happened, and left after the result is not the same as expected. When we assign a value to exports or model.exports, we need to assign a value to ensure consistency, otherwise the last use may not be the same as expected.
It is common to assign values to a variable that causes the referenced object to change the result of the last program run and the expected inconsistency.
Exports and Model.exports in node. js