Module.exports and export in Node.js

This article was translated from: module.exports vs exports in Node.js

I've found the following contract in a Node.js module: I found the following contract in the Node.js module :

module.exports = exports = nano = function database_module(cfg) {...}

Different at The Wonder whats the BETWEEN the I module.exportsand exportsand both-Why are Used here Wallpaper. I do not know the difference between what module.exportsand exportswhy are used here.


#1st Floor

Reference: module-exports and export in https://stackoom.com/question/TwlJ/Node-js


#2nd Floor

I found this link useful to answer the above question. I found this link useful for answering the above question.

http://timnew.me/blog/2012/04/20/exports-vs-module-exports-in-node-js/ http://timnew.me/blog/2012/04/20/exports-vs-module-exports-in-node-js/

To add to the other posts The module system in node does add modules to the system nodes perform other posts

var exports = module.exports 

before executing your code . So when you want to exports = foo, you probably want to do module.exports = exports = foo but using exports.foo = foo should be fine So when you want exports = foo, you may want to execute module.exports = exports = foo, but using exports.foo = foo should be fine.


#3rd floor

Basically the answer lies in what really happens when a module is required via requirestatement. Basically, requirerequire the answer lies in what actually happens when passing a statement module. Assuming this is the first time the module is being required. Assume that this is the first time the module is needed.

For example: For example:

var x = require('file1.js');

contents of file1.js: The contents of file1.js :

module.exports = '123';

When the above statement is executed, an Moduleobject is created. After executing the above statement, an Moduleobject will be created . Its constructor function is: Its constructor is:

function Module(id, parent) {
    this.id = id;
    this.exports = {};
    this.parent = parent;
    if (parent && parent.children) {
        parent.children.push(this);
    }

    this.filename = null;
    this.loaded = false;
    this.children = [];
}

As you see each module object has a property with name exports. As you can see, each module object has a exportsproperty named . What IS IS returned Eventually the this AS Part of require. This is a final requirepart of the return.

Next step of require is to wrap the contents of file1.js into an anonymous function like below: The next step of require is to wrap the contents of file1.js into an anonymous function , as shown below:

(function (exports, require, module, __filename, __dirname) { 
    //contents from file1.js
    module.exports = '123;
});

Anonymous function the this IS the Invoked And at The following Way, modulehere Wallpaper the Refers to at The ModuleObject Earlier the Created. And this anonymous function is called the following way, modulehere refers to Modulethe object you created earlier.

(function (exports, require, module, __filename, __dirname) { 
    //contents from file1.js
    module.exports = '123;
}) (module.exports,require, module, "path_to_file1.js","directory of the file1.js");

See Inside at The CAN WE AS function, exportsformal argument the Refers to module.exports. As we have seen within the function as exportsformal parameters point module.exports. In essence it's a convenience provided to the module programmer. In essence, this is the convenience provided to the module programmer .

However this convenience needs to be exercised with care. However, this convenience needs to be treated with caution. In any case if trying to assign a new object to exports ensure we do it this way. Anyway, if you try to assign a new object to the export, please make sure we adopt this way.

exports = module.exports = {};

WE do following Way IT IF Wrong Way , module.exportsby Will Still pointing to BE AS Part of the Created at The Object Module instance. If we operate the wrong way , module.exportswill point to objects created as part of the module instance.

exports = {};

As as result adding anything to the above exports object will have no effect to module.exports object and nothing will be exported or returned as part of require. As a result, adding anything in the above export object will not produce the module.exports object No impact, and nothing will be exported or returned as part of require.


#4th floor

INITIALLY, module.exports=exports, and at The requirefunction returns A at The Object module.exportsthe Refers to. Initially module.exports=exports, requirethe function returns an object module.exportsreference to the object.

if we add property to the object, say exports.a=1, then module.exports and exports still refer to the same object. If we add properties to an object , such as export.a exports.a=1, module.exports and export still refer to the same object. So if we call require and assign the module to a variable, then the variable has a property a and its value is 1; therefore, if we call require and assign the module to a variable, the variable has the attribute a and its value is 1. ;

But if we override one of them, for example ,, exports=function(){}then they are different now: exports refers to a new object and module. Exports refer to the original object. However, if we cover one of them, such as export exports=function(){}, then they are different now OK: export points to the new object, and module.exports points to the original object. And if we require the file, it will not return the new object, since module.exports is not refer to the new object. If we need the file, it will not return the new object because module.exports does not refer to the new object.

For me, i will keep adding new property, or override both of them to a new object. For me, I will keep adding new properties, or overwriting them all into new objects. Just override one is not right. Just override one is not right . And keep in mind that module.exportsis the real boss. Please keep in mind that it is the real boss .module.exports


#5th Floor

I just make some test, it turns out that, inside nodejs's module code, it should something like this: I just did some tests, and found that in the nodejs module code, it should be like this:

var module.exports = {};
var exports = module.exports;

so: so:

1: 1:

exports = function(){}; // this will not work! as it make the exports to some other pointer
module.exports = function(){}; // it works! cause finally nodejs make the module.exports to export.

2: 2:

exports.abc = function(){}; // works!
exports.efg = function(){}; // works!

3: but, while in this case 3: But in this case

module.exports = function(){}; // from now on we have to using module.exports to attach more stuff to exports.
module.exports.a = 'value a'; // works
exports.b = 'value b'; // the b will nerver be seen cause of the first line of code we have do it before (or later)

#6th floor

in node js module.js file is use to run the module.load system.every time when node execute a file it wrap your js file content as follow node. The module.js file in node js is used to run module.load system. Every time the node executes the file, it will wrap the content of your js file as follows

'(function (exports, require, module, __filename, __dirname) {',+
     //your js file content
 '\n});'

because of this wrapping inside ur js source code you can access exports, require, module, etc .. this approach is used because there is no other way to get functionalities wrote in on js file to another. Since this packaging is in ur js source code , So you can access export, require, module, etc. This method is used because there is no other way to copy the functions written in the js file to another file.

then node execute this wrapped function using c ++. Then the node executes this wrapped function using c ++ . at that moment exports object that passed into this function will be filled .

you can see inside this function parameters exports and module. you can see inside this function parameters exports and module . Actually exports is a public member of module constructor function. In fact, exports is a public member of the module constructor .

look at following code to see the code below

copy this code into b.js copy the code in b.js

console.log("module is "+Object.prototype.toString.call(module));
console.log("object.keys "+Object.keys(module));
console.log(module.exports);
console.log(exports === module.exports);
console.log("exports is "+Object.prototype.toString.call(exports));
console.log('----------------------------------------------');
var foo = require('a.js');
console.log("object.keys of foo: "+Object.keys(foo));
console.log('name is '+ foo);
foo();

copy this code to a.js copy this code to a.js

exports.name = 'hello';
module.exports.name = 'hi';
module.exports.age = 23;
module.exports = function(){console.log('function to module exports')};
//exports = function(){console.log('function to export');}

now run using node now using nodes running

this is the output which is the output

module is [object Object]
object.keys id,exports,parent,filename,loaded,children,paths
{}
true

exports is [object Object] exports is [object Object]

object.keys of foo: name is function (){console.log('function to module exports')} function to module exports foo的object.keys:名称为function(){console.log('function to module导出')} function to module export

now remove the commented line in a.js and comment the line above that line and remove the last line of b.js and run. Now delete the comment line in a.js and comment the line above that line and delete b. The last line of js and run.

in javascript world you cannot reassign object that passed as parameter but you can change function's public member when object of that function set as a parameter to another function In the javascript world, you cannot reassign the object passed as a parameter , but when the function's When the object is set as a parameter of another function, you can change the public members of the function

do remember

use module.exports on and only if you wants to get a function when you use require keyword. Only use module.exports when you want to get the function when you want to use the require keyword. in above example we var foo = require (a.js); in the above example, we var foo = require (a.js); you can see we can call foo as a function; you can see that we can use foo Called as a function;

this is how node documentation explain it "The exports object is created by the Module system. Sometimes this is not acceptable, many want their module to be an instance of some class. To do this assign the desired export object to module.exports." This is how the node documentation explains: "The export object is created by the Module system. Sometimes this is unacceptable, and many people want their module to become an instance of a class. For this, please assign the required export object to the module .exports. "

Published 0 original articles · praised 75 · 560,000 views +

Guess you like

Origin blog.csdn.net/w36680130/article/details/105484246