Example: I go to line 2 below:
Const TCPClient = the require (‘… / modules/tcpClient/index ‘)
So let’s go inside require and debug:
Go to line 11: return mod.require(path);
The value of path is the argument passed in by require:
The value of mode is the module object that currently initiates the require request:
To the Module. _load:
Module._cache checks to see if the Module has been loaded before. If so, it is returned directly from line 479.
Three steps:
- Create a new Module object
- Add the new Module object to the Module cache pool
- Run tryModuleLoad to load the Module
The NodeJS framework tries to load modules from the following seven paths.
This path works the same way as Java classpath:
Read the contents of this module from the file system, store it as a string, and compile:
The return result of require, which is the exports input in our Module implementation file, is the end of require.
For more of Jerry’s original articles, please follow the public account “Wang Zixi “: