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 loading logic of the Node.js module

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces what the loading logic of Node.js module is. It is very detailed and has certain reference value. Friends who are interested must finish reading it.

First, divide the modules according to the mode of organization

File module: as we mentioned in the previous chapter, it is a separate .js file.

Directory module: we can store multiple independent .js files in one directory, that is, in a folder.

Second, the loading logic of the directory module

III. Loading logic of the module

Note: package.json is the description file of the directory module

Modules that are not imported by path will always look for node_moudules, layer by layer. If you can't find it, you'll make a mistake.

4. Example 1

Create 3 new js files in the dir01 directory, namely a.js, b.js, and c.js. Among the three files, the function name is info, and the output is the corresponding file name.

A.js file

B.js file

C.js file

App.js file

Run result (introduced at the beginning of the path)

Error: this module cannot be found for this path. Reason: we have three js files in this directory, and it doesn't know which file you want to call, so it will report an error. Solution: specify the call file of the directory module.

After adding the index.js file, we found that we did not report an error and output index.js, which means that the call file we specified is successful, so we can see that we are calling the index.js file by default. If an error will be reported without this file, how can we change it to call the other three files?

Delete the index.js called by default, create a new package.json file, and then use the "main" attribute to specify the file name of a module. Note: the undefined output here is that our function does not write the return statement, and the default return is undefined.

We take any one of the three file modules, such as a.js, and we can introduce the other two file modules into the a.js file and print them, and we can see the results shown in the figure above.

Do not introduce at the beginning of the path

That is, when require is introduced, it is to write the name of a directory folder directly, not the path, so we need to put this folder under the node_modules directory, otherwise it will report an error, and we will look up the directory with that name when we execute it.

The following is an example of the a.js// import module bdcconst b = require (_ _ dirname+'/b.js') const c = require (_ _ dirname+'/c.js') function info () {console.log (b.info ()); console.log (c.info ()); console.log ('a.js');} / / Export module.exports = {info} b.jsfunction info () {console.log (' b.js') } / / Export module.exports = {info} c.jsfunction info () {console.log ('c.js');} / Export module.exports = {info} app.js// import directory module / / the entry file for the default directory module is index.js// const dir01 = require ('. / custom_module/dir01') / / console.log (dir01.info ()) / / introduction method that does not start with a path const dir02 = require ('dir02') console.log (dir02.info ()); index.js file under the dir02 directory under the node_moudules directory of package.json {"main": "a.js"}

Path: node_moudules/dir02/index.js

Function info () {console.log ('dir02');} / Export module.exports = {info} above is all the content of the article "what is the loading logic of the Node.js module?" Thank you for reading! Hope to share the content to help you, more related knowledge, 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

Development

Wechat

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

12
Report