1. CommandJS is the starting point

1.1. CommandJS module specification

CommandJS module specification is divided into three parts: module reference, module definition, and module identification.

  1. The module reference

The way modules are referenced:

const fs = require('fs')// Node core module

// User-defined file module
const add = require('./add.js') // The.js here can be omitted (more on that later)
Copy the code
  1. Module definition/module export export file throughexportsObject exports a method or variable from the current module.
//math.js 
exports.add = function(){}

// add.js
exports.add = function(){}
Copy the code
  1. Module identifier The module identifier is the argument passed to the require() method, which requires a small hump name (example :setName), or a. Or.. A relative or absolute path at the beginning.

2. Module implementation of Node

Node exports modules through exports, require and module. But how does it work internally? Here's what Node does to implement it.Copy the code

1. Path analysis 2. File location 3

  • There are two types of modules in Node:The core module,File module
  • The core module is compiled into binary files during the compilation of NOde source code. When NOde starts, the core part is loaded directly into memory, so when this part of the core module is introduced, the file location and compilation execution steps are omitted, so the loading speed is the fastest.
  • File module is dynamically loaded at run time, which requires complete path analysis, file location, compilation and execution.

2.1 Preferentially load from cache

In the same way that browsers cache JS files to reduce access to data resources, Node modules cache data resources. Files that are first required into the environment are cached in 'module.caches []'. Caching takes precedence over core modules and file modules.Copy the code

2.2 Path Analysis and File Location

The parameters in require() mentioned earlier are module identifiers, and module lookup and location are different depending on the identifiers introduced.Copy the code

1. Module identifier analysis

  • Core modules: such as FS, PATH, etc

  • . Or.. Start the relative path file

  • Non-path file modules, such as your own add.js module

  • Core modules:

The core module takes second priority to cache loading because it is already compiled into binary code and loads quickly. Note: if you want to write a module with the same name as the core module, it will not load successfully.

  • Path form module

.,.. And/start identifiers are treated as file modules. When parsing path modules, require() will convert the path to the real path, find the file, compile and execute it, and put it in the cache to make the second load faster. There is a need to find the location of the file, so the search process can save a lot of time and load slower than the core module.

  • Custom modules

A custom module is not a core or a file module, but may be a special file module, in the form of a file or package, which is the most cumbersome.

  • To view the module path of the current file, use the output module.paths.
[
'/Users/cookie/Desktop/Leetcode/Node/node_modules'.'/Users/cookie/Desktop/Leetcode/node_modules'.'/Users/cookie/Desktop/node_modules'.'/Users/cookie/node_modules'.'/Users/node_modules'.'/node_modules'
]
Copy the code

The path rule is node_modules in the parent directory of the current file node_modules in the grandfather directory node_modules… Here, just like the prototype chain, you look inside out until you find it

2. Locate files

The optimization strategy loaded from the cache can eliminate the need for path analysis, file location and compilation execution during the second introduction, greatly improving the efficiency of module reloading.

  • File extension name analysis

Require () will use the order.js=.json=.node if no suffix is declared during the analysis of identifiers. Here’s a tip: when importing.json and.node files, it’s a good idea to add suffixes. This will speed up the loading of these files.

2.3 Module Compilation

File compilation and execution is the last stage of importing modules, and Node loads and handles files differently for different files.Copy the code
  • .js file: PassfsThe module reads the file and compiles.
  • Json file: YesfsAfter the module reads the file, useJSON.parse()Export after processing.
  • .node file: This is an extended file written in C/C++ that loads the final compiled file through dlopen().

Thank you

This article is the record after worship big guy’s works, not the small dish chicken’s own research, only as a record, urge themselves to study well, thank you!! 🙏