This is the 13th day of my participation in the August More Text Challenge. For details, see:August is more challenging

preface

The module loading of Node adopts CommonJS specification, which is different from the asynchronous loading of AMD. CommonJS is synchronous loading, because the Node code is generally deployed on the server, and the modules it depends on also exist on the server. Therefore, it is equivalent to the local module loading, and the loading speed is fast.

CommonJS uses the require directive internally to load modules, and the module.exports or exports directive to export modules.

module.exports

In node, each file is a module. A module contains a Module object, and it has a property, exports, which represents the interface exported. Values that need to be output are assigned to this property and can be accessed via require.

exports

In order not to write module. This directive every time, CommonJS provides an easy way to write only exports for module.exports. This is a handy instruction.

Similarities and differences between

We can print it out

Create a new index.js file

console.log(module.exports) / / {}
console.log(exports) / / {}
console.log(module.exports === exports) // true
Copy the code

If you execute node index.js, you can see that all of them are printed with {}, which means that the default value is {}, and that they are all equal, which means that they refer to the same object.

Let’s take a look at how they’re used:

Exports of usage

main.js

exports.name = 'the answer cp3'
Copy the code

use.js

const obj = require('./main.js')
console.log(obj.name) / / the answer cp3
Copy the code

The use of the module. Exports

main.js

module.exports.name = 'the answer cp3'
Copy the code

use.js

const obj = require('./main.js')
console.log(obj.name) / / the answer cp3
Copy the code

There is no problem with this use, can print out the answer CP3.

Let me write it another way so you can see the difference

Exports of usage

main.js

exports = {
  name: 'the answer cp3'
}
Copy the code

use.js

const obj = require('./main.js')
console.log(obj) / / {}
console.log(obj.name) // undefined
Copy the code

The use of the module. Exports

main.js

module.exports = {
  name: 'the answer cp3'
}
Copy the code

use.js

const obj = require('./main.js')
console.log(obj) // {name: 'answer cp3'}
console.log(obj.name) / / the answer cp3
Copy the code

As you can see,

  • The first oneexportsThe usage of the print isundefined
  • The secondmodule.exportsThe usage of the print isThe answer to cp3

Why is that? Shouldn’t they all be the same?

Let’s look at another example:

main.js

exports.a = {
  name: 'the answer cp3'
}
module.exports = {
  name: 'the answer cp3'
}
Copy the code

use.js

const obj = require('./main.js')
console.log(obj) // {name: 'answer cp3'}
Copy the code

In this example, only {name: ‘answer cp3’} is printed, and the exports.a attribute is ignored.

All right, so to summarize this example.

  • The module object initializes with the exports attribute and assigns it {}. It declares an exports variable and assigns the exports attribute to it.

    Similar to the

    var module = {
      exports: {}}var exports = module.exports
    Copy the code
  • Exports are made using module.exports, not exports.

    So once we re-assign exports or module.exports, we are cutting off the reference from the other side. The two are not related.

    Such as:

    var module = {
      exports: {}}var exports = module.exports
    
    // Exports
    exports = {
      name: 'the answer cp3'
    }
    console.log(module.exports)  // The object is still empty
    Copy the code

    So we

    • Or just usemodule.exportsThis property, noexports
    • Or just giveexportsAdd attributes, do not reassign

conclusion

  • module.exportsThe initial value is an empty object{}
  • exportsIs pointing tomodule.exportsA reference to the
  • And the derived one is just onerequireEverything that’s imported is pointing tomodule.exportsattribute
  • rightexportsormodule.exportsA new assignment breaks the referential relationship