SoFunction
Updated on 2025-03-02

Analysis of the functional understanding and usage of module module

This article describes the functional understanding and usage of module modules. Share it for your reference, as follows:

The CommonJS specification is used to implement module functions, and a separate file is a separate module. Dependency management between modules is realized through the require method.

Loading the module through requires is a synchronous operation.

The loading process is as follows:

1. Find the module file that needs to be loaded.

2. Determine whether it has been cached. If not, read the contents of the module file.

3. Encapsulate the read content in a function and run it.

(function (exports, require, module, __filename, __dirname) {
  //The module's code is actually here});

4. Return the content that needs to be exported.

exports represents the export object of the module

require means require method

module represents the current module object

__filename represents the absolute path to the current file

__dirname represents the absolute path to the current folder

Inside the module, this points to the export object of the current module

(this === );
(this === exports);

The module object has many properties, you can print it out and take a look.

(module);

Indicates the identifier of the module.

Represents the export object of the module.

Represents the parent module of the current module and who loads the current module.

Indicates the absolute path to the module.

Indicates whether the loading is complete.

Indicates which modules are loaded by the current module.

Represents the search path of a module, and the amount of the path depends on the depth of the directory.

Loading files through the require method is synchronous. In order to improve efficiency, the loaded files will be cached.

();

The cached key is the absolute path of the module, and the value is the module object.

Gets the absolute path to the module, but does not load the module.

(('./'));

Get the entry module

();

Get the extension supported by the module

There are three types of module files:
1. .js module
2. .json file module
3. Node C++ extended binary module

();

When loading a file through require, for example, require('./user') will first look for the user file, then not find it, then look for , then look for , then look for , then look for .

Module classification:

1. Core module, compile binary, and load the fastest, such as fs, http, events, etc.

2. File module, saved on the hard disk, is loaded slowly, and is loaded by name or path.

3. For third-party modules, only specify a name, they will be loaded from the node_modules directory. The query path includes and global directory.

Global Directory:

In windows: the path specified by the environment variable NODE_PATH.

In linux: $HOME/.node_modules and $HOME/.node_libraries directories

Rules for file module search, such as require('./user'):

1. Determine whether the loaded file is loaded with an absolute path.

2. If it is an absolute path, the file with the absolute path will be loaded directly.

3. If not, determine whether the file has a starting point with './' , '../' . If there is, find the relative path. If not, get the path through and the global directory.

4. Try to find the user file under the path in step 3.

5. If not found, try adding the extension ( , ,) to search.

6. If not, try to search based on the package. In the user directory, the main configuration file in the package file description is loaded.

7. If not, look for the user directory, and .

8. If not, look for the next one.

9. If it is not found in the end, the file fails to load.

I hope this article will be helpful to everyone's programming.