Modules for Nodejs

Source: Internet
Author: User

The introduction of modules in node requires the following 3 steps: (1) Path Analysis (2) file positioning (3) compile execution

The modules in node are divided into two categories:

One is the module that node provides-the core module. This part is compiled into the binary file during the compilation of node source code. When the node process starts, some core modules are loaded directly into memory, so when this part of the core module is introduced, the file location and compilation execution is omitted and the load speed is the fastest in the path analysis.

The second is the user-written module-the file module. Dynamic loading at runtime requires complete path analysis, file positioning, and compilation execution, which is slower than the core module.

1, priority loading from the cache

Node caches the introduced modules to reduce the overhead of two ingestion. The difference is that the browser caches only the files, and node caches the objects after they are compiled and executed. The cache check of the core module precedes the cache check of the file module.

2, the module path module path is node in the location of the file module specific files specified when the search strategy, in particular, a path composed of an array.

// module_path.jsConsole.log (module.paths);

Executed under Windows:module_path.js file

" D:\Program files\nodejs\module_path.js "  'd:\\program files\\nodejs\\node_modules',  'd:\\ Program Files\\node_modules',  'd:\\node_modules'  ]

You can see the generation rules for the module path:

The Node_modules directory under the current file directory; The Node_modules directory under the parent directory; The deferred path is recursively progressive, knowing the Node_modules directory under the root directory.

3. Document Location

File extension Analysis

Require () during parsing the identifier, if the identifier does not contain a file extension, node will make up the extension in the order of. Js\.json\.node, and then try again.

Calling the FS module synchronously blocks The attempt to determine whether the file exists. Because node is single-threaded, if it is a. node and. json file, with the extension in the identifier passed to require (), it will speed up a bit. Another trick is to synchronize with the cache , which can significantly alleviate the pitfalls of blocking calls in node single-threaded.

4. Module Compilation

Each file module is an object, which is defined as follows:

 function   Module (ID, parent) { this . ID = ID;      {};      parent;         if  (parent && Parent.children) {    Parent.children.push ( this  );  this . FileName = null  ;  false  ;  this . Children = [];}  

When you navigate to a specific file, node creates a new module object and then loads and compiles it according to the path.

Modules for Nodejs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.