Module Loading process:
Path analysis, file positioning, module compilation
Node caches the introduced modules to reduce the overhead of two ingestion. The cache is compiled and executed after the object. Require is the first priority for modules in the cache
Path analysis
Module identifiers: Require parameters, which can be divided into the following categories according to the written form:
- Core modules: such as Http,fs,path
- File module
- Path module
- Relative path module:. Or.. Begin
- Absolute path module:/Start
- Modules in non-path form
Core modules
- Compiled into binary code during node. JS source code compilation, the loading speed is very fast;
- The priority is second only to the cache, so the file module with the Core module identifier cannot be loaded successfully.
File modules in path form
- The module compiles and executes only when require;
- is converted to a real path, and the compiled execution object is placed in the cache with a real path as the index.
File modules in non-path form
- Starting from the current module, always follow the path upward recursively, until the root directory, looking for node_modules directory for file positioning;
File location
When parsing identifiers, the file extension is parsed, the corresponding file is not found, but a directory is processed as a package.
File extension Analysis
require
When the identifier does not need to include the file name extension, node .js,.json,.node
synchronizes the location in the order in which it is located.
Determines whether a file exists synchronously, so adding an .json
.node
extension when introduced will speed up the introduction
Directory Analysis and Packages
Module compilation
After the file module is positioned successfully, node creates a new module object and then loads and compiles it according to the path. Depending on the extension of the file, the method of loading it is different.
//module Objectfunction Module(ID,Parent{ This.ID =Id; This.exports = {}; This.Parent =Parent; Updatechildren(Parent, This, false); This.filename = NULL; This.Loaded = false; This.Children =[];}
You can know how to extend the system by require.extensions. You can even use the form of require.extensions['. Ext ' to customize how the. ext extension is loaded, although it is officially discouraged and is recommended to be compiled into JavaScript files first.
. js file
Read the files synchronously through the FS module and compile the execution;
The file content is first and foremost packaged:
(function,,,, __dirname){ **JavaScript content**});
This can be done by:
- Introduce variables in the file
exports , require , module , __filename , __dirname
;
- Scope isolation is performed between each module file;
The module after the package will be given to Vm.runinthiscontext to perform a function (note that function). Finally, the Module.exports,require,module and the __filename and __dirname obtained in the file location are passed as parameters to this function
execution.
So, assign a value to the exports within the module:
=function(){};
is a reference to a formal parameter that alters an anonymous function, but neither the module nor the module.exports in the argument can be known to be assigned.
. Node
This is an extension file written in C/s + +, which is loaded with the Dlopen () method to generate the last compiled file.
. JSON
After the file is read synchronously through the FS module, the result is parsed with json.parse () and then assigned to the exports of the module object.
Each successfully compiled module caches its file path as an index on the Module._cache object to increase the performance of the two introductions.
Reference:
http://nodejs.cn/
http://www.ituring.com.cn/book/1290
Node. JS's module mechanism