What is the difference between module.export and export

What is the difference between module.exportand export?

What if there are some attributes in the module.export object? Will it be export.xxinvalid then?

+1
source share
3 answers

First of all, it is exportsand module.exportsnot, exportand module.export(there is also a keyword in JavaScript export, but it is not yet supported in Node).

Each Node module is wrapped with this function:

(function (exports, require, module, __filename, __dirname) {
  // Your module code actually lives in here
});

See: https://nodejs.org/api/modules.html#modules_the_module_wrapper

, . .

module.exports exports , < module.exports , , .

, :

module.exports.x = 1;
# or:
exports.x = 1;

.

:

module.exports = {x: 1};

:

exports = {x: 1};

x, exports ( module.exports), module.exports .

module.exports = {x: 1}; , , . , changes the property module, , .

:

exports = {x: 1};
module.exports = exports;

exports , module.exports.

, module.exports, , :

exports.x = 1;
module.exports.y = 2;

. :

+1

- - () .

const module = { 
   //property
   exports: {} 
}; 

- module.exports,

  • export === module.exports

    module.exports.hello = true;
    exports.hello = true; 
    
  • export! == module.exports

    module.exports.hello = true;
    exports = { hello: true };
    

    .

function require(/* ... */) {
   const module = { exports: {} };
   ((module, exports) => {
      // Your module code here. In this example, define a function.
      function someFunc() {}
      exports = someFunc;
      // At this point, exports is no longer a shortcut to 
      // module.exports, and
      // this module will still export an empty default object.
      module.exports = someFunc;
      // At this point, the module will now export someFunc, instead of 
      //the default object.
    })(module, module.exports);
   return module.exports;
 }
+1

module.exports require, script, , , exports, , module.exports . module.exports, , , , , ?

, exports - , , , script tada.js:

exports = function(){
    console.log('Tada');
}

console.log(exports);
console.log(module.exports);

script app.js:

var tada = require('./tada');

?

, :

console.log(exports); // [Function]
console.log(module.exports); // {}

exports module.exports . , , , , , ? , , , JavaScript .

JavaScript, = , , , , , reference exports module.exports , exports module.exports , :

exports = function(){
    console.log('Tada');
}

, exports .

also remember that to return a function module.exports, the return function is not exportsrequired, so we got an empty object for module.exportsin our example, and after the link is broken module.exports, they exportsno longer point to the same object.

To fix this, you need mutateto overwrite instead. so if we change our example to this, both exportsand module.exportswill have the same value:

exports.tada = function(){
    console.log('Tada');
}

console.log(exports);
console.log(module.exports);
0
source

Source: https://habr.com/ru/post/1664542/


All Articles