nodejs custom module lookup rules

nodejs custom module lookup rules

node identifier module looks require the use of priority
  1. The core module, such as http, fs, path and so on.
  2. In. Or a relative path starting ... modules.
  3. Begin with / absolute path to the file module.
  4. Non-file path in the module, as the custom modules connect.
Core Module

The core module priority only after the loading buffer, which has been compiled into binary code in the source code of the node, so the loading process fastest.

Path in the file module

To. ... and identifier / start of /, here it is treated as a file module to handle. In the analysis path module, require () method sets the path into the true path, and as an index to the true path, the path will be compiled as an index, compiled the results after execution stored in the cache so that the secondary load faster .
* Because the file module indicates the exact location of the file to the node, so the discovery process can save a lot of time, it loads slower than the core module.

Custom Modules

It means a non-custom module core module, not in the form of a path identifier. He is a special file module, it may be in the form of a file or package. Find this type of module is the most time-consuming, but for all modules in the slowest.
Before the introduction of self-defined way to find the module, we need to introduce the concept of path module.
Module Path node search strategy is developed in locating files module, manifested in the array is composed of a path.
Module path generation rule as follows:

  • Node_modules current directory under the file directory.
  • node_modules directory under the parent directory.
  • node_modules directory under the parent directory parent directory.
  • Progressively higher along the radial recursively until node_modules directory under the root directory.
    His way to find ways to generate prototype js chain or scope chain is very similar. In the loading process, node will try one by one path module path until you find the target file. As can be seen, the deeper the current path of the file, the module will be more time-consuming to find, which is self-defined module loading speed of the slowest reason.
//新建一个index.js
console.log(module.path);
//输出该文件的路径数组
File Positioning

Optimization strategies loaded from the cache so that the secondary load path does not require the analysis, documentation and localization process compiler implementation, greatly improving the efficiency of the load module again.
But in the process of locating the file, there are some details that need attention, including the processing and analysis of file extensions, directories and packages.

  • File extension analysis
    require () process analysis identifiers, there will be some cases it does not include extension. CommonJS module also allows specification does not include the file extension identifier in the identifier, in this case, node and will press .node .json file, passed to require () the identifier of the identifier strip tape extended name, will speed up a little speed. Another trick is: sync with the cache can significantly alleviate the defect called when the node in single-threaded blocked.
  • Directory packet analysis and
    often when the analysis process identifiers, the require () after analysis by extension, may not find the corresponding file, but give a directory, to find that the introduction of custom modules and module-by path appears, at this time node directory will be treated as a package to deal with.
    In this process, node package specification for CommmonJS a certain degree of support. First, node looks in the current directory package.json (CommonJS package specification defines the package description file) parses out the package description of the object by JSON.parse (), to find out the main attribute specifies the name of the file to locate. If the file name extension is missing, the extension will enter the analysis step.
    If the main attribute specifies the file name wrong, or are they not package.json file, node will index as the default file name, and then click Find Now index.js, index.node, index.json.
    If you do not locate the success of any file in the directory analysis, the custom module to the next module path to find it. If the module array of paths are traversed, still did not find the target file, the failure of an exception is thrown.
Published 27 original articles · won praise 5 · Views 6118

Guess you like

Origin blog.csdn.net/systempause/article/details/104298443