Share members in module

  1. module object

    In each .js custom module there is a module object, which stores information about the current module. After printing there is:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    Module {
    id: '.',
    path: 'D:\\Front-end\\Nodejs',
    exports: {}, #Important attributes, members can be shared externally through exports, the default value is an empty object.
    filename: 'D:\\Front-end\\Nodejs\\prac1.js',
    loaded: false,
    children: [
    Module {
    id: 'D:\\Front-end\\Nodejs\\prac1a.js',
    ... #here omitted
    }],
    paths: [
    'D:\\Front-end\\Nodejs\\node_modules',
    'D:\\Front-end\\node_modules',
    'D:\\node_modules'
    ]
    }

  1. module.exports object

    In a custom module, you can use the module.exports object to share the members of the module for external use. The default value is {}.

    Outside When importing a custom module with the require() method, what you get is the object pointed to by module.exports. If the custom module does not export any objects, the result of printing require('./js1') from the outside world is {}, which is the default value of exports.


    Ways to share members:

    1
    2
    3
    4
    module.exports.username = 'Derek' //Mount properties to the module.exports object
    module.exports.sayHello = function(){ //mounting method
    console.log('Hello!')
    }

    Note that the shared members here exist as attributes of module.exports. If you define a variable/function first, and then share the variable/function name, it cannot be shared.


    Note: When using the require() method to import a module, the result of the import is always based on the object pointed to by module.exports. That is to say, if you define some properties of module.exports first, and then assign a new object to module.exports, the result of the export is a new object (because the new object is new address); whereas if you first assign a new object to module.exports and then add some properties, it will export the new object with the added properties.


  2. exports object

    Since module.exports is complicated to write, in order to simplify the code, Node.js provides the exports object. By default, exports and module.exports point to the same object. The default final shared result is still based on the object pointed to by module.exports.


    Mistakes in using module.exports and exports:

    When you use .attribute = to attach objects, the two are common; but when you use .exports = {} to directly assign objects, exports are created by yourself, which is directly related to module.exports The given objects are not the same. If you use .exports = {} to directly assign the object, and then want to use module.exports.attribute = to add attributes, then you should use module.exports = exports to point to exports The object is assigned to module.exports.

    To prevent confusion, it is recommended not to use both module.exports and exports in the same module.


Share