The import and export rules and principle analysis of modules in Node.js

Problem Description

This blog will solve the following problems:

  • The connection and difference between module.exports and exports
  • Import and export rules of modules in Node
  • Principle analysis, deep understanding of essence

1. Introduction to modules in Node

Node provides many server-level APIs for JavaScript, and most of these APIs are packaged in a named core module.

  • For example, the fs core module for file operations, the http module built by http services, the path operation module, and the os operating system information module
  • There is no global scope in node, only module scope

The require method has two functions

  • Load the file module and execute the code in it
  • Get the interface object exported from the loaded file module

There are three types of modules in Node

  1. Core modules : provided by Node itself, such as fs file operation module, http network operation module
  2. Third party modules : provided by a third party, use of the time we need to npmbe downloaded before it can be loaded using
  3. Modules written by users : it is not easy to write and maintain when we write a lot of code in files, so we can consider splitting the code in the file into multiple files, then these files created by ourselves are user modules

2. Import and export rules

2.1 Export multiple members

method one:

//导出
module.exports.a = 'hello';
module.exports.add = function () {
    
    
  console.log('add');
}

//导入
let test = require('./a.js')
console.log(test);//{ a: 'hello', add: [Function] }

Because there are a lot of writing on top of the kind of point to point, too much trouble, and therefore provides the Node exportsobject, the default exports 和 module.exportspoint to the same reference

Method Two:

//导出
exports.a = "hello";
exports.add = function () {
    
    
  console.log("add");
}

//导入
let test = require('./a.js')
console.log(test);//{ a: 'hello', add: [Function] }

Method three (recommended):

//导出
module.exports = {
    
    
  a: 'hello',
  add: function () {
    
    
    console.log('add');
  }
}

//导入
let test = require('./a.js')
console.log(test);//{ a: 'hello', add: [Function: add] }

2.2 Export a single member

There is only one way, only usemodule.exports = ***

//导出
module.exports = 'hello';

//导入
let test = require('./a.js')
console.log(test);//hello
//导出
module.exports = 'hello';

//后者会覆盖前者
module.exports = function () {
    
    
  console.log('add');
}

//导入
let test = require('./a.js')
console.log(test);//[Function]

Why exports = ***can't I export a single member? Let's try

//导出
exports = 'hello';

//导入
let test = require('./a.js')
console.log(test);
//{} 结果是是一个空对象

3. Principle analysis

  • In Node, inside every module, there is an object module
  • In the module object, there is an attribute exports, exports is also an object
var module = {
    
    
    exports: {
    
    }
}
  • As well as a member of the module exportsis equivalent tomodule.exports
var exports = module.exports
console.log(exports === module.exports) // => true
  • By default, there is a sentence at the end of the code: In return module.exports; other words, the last exportedmodule.exports , we use and requireget module.exports, the above rules are all implemented at the bottom level, we can't see it.

  • Code demo

// 把下面这些代码想象成模块内部的底层实现

// 每个模块内部都有一个 module 对象
// module 对象中有一个成员 exports 也是一个对象
var module = {
    
    
  exports: {
    
    }
}

// 模块中同时还有一个成员 exports 等价于 module.exports
var exports = module.exports

console.log(exports === module.exports) // => true

// 这样是可以的,因为 exports === module.exports
// module.exports.a = 123
// exports.b = 456

// 对exports重新赋值会断开和 module.exports 的连接,
// 因此这样并不能导出单个成员,因为模块最后 return 的是 module.exports
// exports = function () {
    
    
// }

// 这才是导出单个成员的正确方式
module.exports = function () {
    
    
  console.log(123)
}

// 最后导出的是 module.exports
return module.exports

Let me explain it by drawing a picture:

(1) no re-assignment of exports, default module.exportsand exportspoint to the same object in memory.
Insert picture description here
(2) re-assignment of exports, exportsthe disconnection and module.exportsconnection
Insert picture description here
(3) We can also re-establish the connection relationship references, useexports = module.exports


4. Extended reading

https://blog.csdn.net/weixin_43974265/category_10692693.html

Guess you like

Origin blog.csdn.net/weixin_43974265/article/details/111823622