An overview of the

Node applications are made up of modules. A file is a module with a separate scope. Variables, methods and classes declared in the file are private and must be exposed using module.exports or exports if they need to be accessed externally

The module object

Node provides a Module builder function inside. A Module is an instance of a Module.

function Module(id, parent) {
  this.id = id;
  this.exports = {};
  this.parent = parent;
  // ...
Copy the code

Each module has a module object inside it. Module represents the module itself, and its exports property (module.exports) is the external interface. Loading a module loads the module.exports property of that module.

Module. id The module's identifier, usually the module's file name with an absolute path. Module. filename specifies the filename of the module, with an absolute path. Module. loaded Returns a Boolean value indicating whether the module has completed loading. Module.parent returns an object representing the module that called the module. Module. children returns an array representing other modules used by this module. Module. exports indicates the value that the module exports.Copy the code

The require method is used to load modules, that is, the module.exports object

The sample

It can be understood in combination with the following example

// example.js
let baseNum = 1;
let addOne = function (num) {
  return baseNum + num;
}

module.exports.baseNum = baseNum;
module.exports.addOne = addOne;
console.log('example', module)
Copy the code
// main.js
let example = require('./example.js')
console.log(example)
console.log(example.baseNum);
console.log(example.addOne(5));
Copy the code

Example. js and main.js are two separate files, which can be regarded as two modules. Under normal circumstances, baseNum and addOne in example.js cannot be accessed from main.js. The example file uses module.exports, so it is accessible from main.

The module in example can be seen as an instance of the example.js module itself.

// console.log('example', module)
example Module {
  id: '.',
  exports: { baseNum: 1, addOne: [Function: addOne] },
  parent: null,
  filename: '/home/user/Desktop/nodetest/example.js',
  loaded: false,
  children: [],
  paths:
   [ '/home/user/Desktop/nodetest/node_modules',
     '/home/user/Desktop/node_modules',
     '/home/user/node_modules',
     '/home/node_modules',
     '/node_modules' ] }
Copy the code

Module. exports: node main.js; example: exports: node main.js;

/ / example. Js console output Module {id: '/ home/user/Desktop/nodetest/example, js', exports: {baseNum: 1, addOne: [Function: addOne] }, parent: Module { id: '.', exports: {}, parent: null, filename: '/home/user/Desktop/nodetest/main.js', loaded: false, children: [ [Circular] ], paths: [ '/home/user/Desktop/nodetest/node_modules', '/home/user/Desktop/node_modules', '/home/user/node_modules', '/home/node_modules', '/node_modules' ] }, filename: '/home/user/Desktop/nodetest/example.js', loaded: false, children: [], paths: [ '/home/user/Desktop/nodetest/node_modules', '/home/user/Desktop/node_modules', '/home/user/node_modules', '/home/node_modules', '/node_modules']} // main.js console.log(example) {baseNum: 1, addOne: [Function: addOne] } 1 6Copy the code

Exports variable

For convenience, Node provides an exports variable for each module, pointing to module.exports. Var exports = module.exports; var exports = module.exports; var exports = module.exports; , so the above example.js can be changed to:

// example.js
let baseNum = 1;
let addOne = function (num) {
  return baseNum + num;
}

exports.baseNum = baseNum;
exports.addOne = addOne;
console.log('example', module)
Copy the code

Note that the following two approaches are wrong

exports = addOne; // exports reassigned, no longer pointing to module.exportsCopy the code
exports.addOne = addOne; module.exports = 'hello'; Exports refers to module.exports. Module. Exports refers to a string, so addOne is not accessible from outsideCopy the code