Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the principle of module mechanism in Node.js?

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you what the principle of the module mechanism in Node.js is, the content is concise and easy to understand, and it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Module specification of Node

In fact, the definition of module is very simple, mainly divided into three parts: module reference, module definition and module identification.

1) Module reference

There is a require () method in Node.js, which accepts the module identity to introduce a module's API into the current context.

2) Module definition

Since we can use require () to introduce the module, we can naturally lead to the module. Node.js provides the methods and variables that the exports object uses to export the current module, and exports is the only export. In each module, there is a module object that represents the module itself, and exports is actually an attribute of module. In Node.js, a file is actually a module, which can be exported by binding the methods and properties we need to export to the exports object as properties.

In another module, you can introduce the module through require (), and you can use the exported method sum ().

3) Module identification

The module ID is actually passed to the require () method. The module ID must be a string that conforms to the hump name or a path that begins with. /,.. /. The .js suffix can be omitted by introducing the module ID.

The advantage of the module is that specific methods and variables are limited to a specific scope, so that developers do not have to think about variable pollution at all.

Module implementation of Node.js

In Node.js, there are three types of modules, one of which is the core module provided by Node.js, such as the fs file module and database database module mentioned in the previous article, and the other is the file module written by the developer, such as the test.js module in the example just now, and the third type is the custom module, which is a special file module, which is generally in the form of a file or package, such as the jar package needed to introduce mysql.

There are three steps to introduce a module into Node.js:

(1) path analysis

For the file module, the module identification indicates the exact file location when it is introduced, so a lot of time can be omitted in the path analysis, and the loading speed is second only to the core module.

The custom module compares the paths one by one from the root of the project until the target module is found. Therefore, the deeper the path of the custom module, the more time-consuming the path analysis, so the loading speed of the custom module is the slowest.

(2) File location

In fact, as mentioned just now, module identification can not contain suffixes, so Node.js will add .js, .json, .node suffixes in turn when locating files, and then go to locate files. Because Node.js is single-threaded, blockage will occur during file positioning, so if the introduced module suffix is .json or .node, you can add a suffix when it is introduced to improve the search speed.

(3) compile and execute

Once defined to a specific file, Node.js creates a module object, then introduces the module and compiles it. The file path of each compiled module will be cached as an index on the cache object to improve the performance of the secondary introduction module.

In the process of compiling the Node.js source code, the core module is directly compiled into a binary file, and then directly loaded into memory, so when the core module is introduced, the two steps of file location and compilation execution can be skipped directly, and the core module will be judged first in the path analysis, so the loading speed of the core module is the fastest.

The file module is loaded dynamically during execution, so path analysis, file location and compilation can not be omitted, so the loading speed is slower than the core module.

Node.js caches the introduced modules to reduce the performance overhead of the secondary introduction module. All the secondary loading modules adopt the cache priority mode. Cache checking of the core module takes precedence over the file module.

Package Management tool NPM

Just now we talked about the Node module, but although we can reference the module, the module and the module are still scattered around, and can not be directly referenced from each other. On the other hand, Node's package management tool NPM connects modules to each other. The package is actually to further organize the JavaScript code on the basis of the module.

In fact, NPM will have a package description file package.json, which is usually located in the root directory of the package. All the behaviors of NPM are closely related to the package description file. As mentioned in previous articles, NPM, as the default package management tool, will be installed together as a Node environment.

Common functions of NPM

NPM helps Node complete the release, installation and dependency of third-party modules. Because of the existence of NPM, a good ecosystem has been formed between Node and third-party modules, and it is becoming more and more powerful. Next, we will outline a few common NPM commands.

Npm-- version to view the current version of NPM

Npm View help instructions

Npm help to view specific command instructions

Executing the command opens the documentation for the corresponding command in the browser

Npm install installs the dependency package, which defaults to the-- save parameter, which is added to the package.json by default.

By executing this command, NPM creates a node_modules directory in the current directory, then node_modules creates a directory for the corresponding dependency package, and then unzips the dependency package to that directory.

Npm init initializes the generation of package.json files in this directory

7.npm uninstall uninstalls dependent packages. By default, it uses the-- save parameter, which is removed from package.json.

8.npm ls to view the dependent packages of the current directory

9.npm root-g View the global installation address

10.npm list to view the current version of the dependency

Problems in NPM

On the NPM platform, everyone can share the package, so there is no way to guarantee the quality of the package, and Node.js runs on the server side, so security needs to be considered. So a good module needs to meet several major modules:

(1) have good testing

(2) have good documentation

(3) have good test coverage.

(4) have good code specification

The above is what is the principle of module mechanism in Node.js. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report