学习笔记—Node中模块查找机制(Learning notes – module search mechanism in node)

日常的学习笔记,包括 ES6、Promise、Node.js、Webpack、http 原理、Vue全家桶,后续可能还会继续更新 Typescript、Vue3 和 常见的面试题 等等。

日常的学习笔记,包括 ES6、Promise、Node.js、Webpack、http 原理、Vue全家桶,后续可能还会继续更新 Typescript、Vue3 和 常见的面试题 等等。

在学习其他模块之前,我们先要清楚 文件模块中的查找规范机制 。

自定义模块查找规范

引用自定义模块时,会自动查找目标目录下的 index.js文件 。

// /jQuery/index.js
module.exports = "jquery"
// /main.js
const jQuery = require('./jQuery')
console.log(jQuery); // jquery

但是如果我们不去定义 index.js,则会出现无法查找文件的问题,也就是报错。

// /jQuery/a.js
module.exports = "jquery"
// /main.js
const jQuery = require('./jQuery')
console.log(jQuery); // Cannot find module './jQuery'

这样我们就需要一个描述文件 ,在文件中有一个 属性,可以定义文件入口。

package.json
main
// /jQuery/package.json
{
    "main":"./jQuery/a.js"
}

这样我们再重新打印,发现结果被成功打印了出来。

所以我们可以总结一下文件模块的 查找规范

  • 在 最新的node版本中,默认 先查找同名文件 。如果没找到,则尝试添加查找 .js和.json文件 。如果还是没有,会查找 同名文件夹(当成了一个包),找到后,会先找 package.json文件。如果没有,则会继续查找 index.js文件。如果依旧不存在,则会报错。
  • 在 老版本的node中, 如果 有package.json 的情况下,会先找同名包。如果 没有package.json,会去 查找同名文件。
// 新版node查找流程
│
└───┤ 同名文件
 │	
 │──┤ 同名.js / 同名.json
 │
 │──┤ 同名文件夹
    │
    │───┤ pageage.json
    │
    └───┤ index.js
   
 都没找到,则抛出一个错误。

这就是node的自定义模块查找规范,我们在平时面试的时候可能会遇到这种问题。

比如介绍一下 node的模块查找机制,我们就可以用这种方式去介绍。(目前一般很少有问老版本node的查找机制,一般都会问两种查找机制的区别,感兴趣的可以去查阅相关资料。)

本篇文章由莫小尚创作,文章中如有任何问题和纰漏,欢迎您的指正与交流。
您也可以关注我的 个人站点、博客园 和 掘金,我会在文章产出后同步上传到这些平台上。
最后感谢您的支持!

本篇文章由莫小尚创作,文章中如有任何问题和纰漏,欢迎您的指正与交流。
您也可以关注我的 个人站点、博客园 和 掘金,我会在文章产出后同步上传到这些平台上。
最后感谢您的支持!

————————

Daily learning notes, including ES6, promise, node.js, webpack, HTTP principle, Vue, etc. typescript, vue3 and common interview questions may be updated later.

Daily learning notes, including ES6, promise, node.js, webpack, HTTP principle, Vue, etc. typescript, vue3 and common interview questions may be updated later.

Before learning other modules, we should first understand the search specification mechanism in the file module.

Custom module lookup specification

When you reference a custom module, you will automatically find the index.js file in the target directory.

// /jQuery/index.js
module.exports = "jquery"
// /main.js
const jQuery = require('./jQuery')
console.log(jQuery); // jquery

However, if we do not define index.js, the file cannot be found, that is, an error will be reported.

// /jQuery/a.js
module.exports = "jquery"
// /main.js
const jQuery = require('./jQuery')
console.log(jQuery); // Cannot find module './jQuery'

In this way, we need a description file with an attribute in the file to define the file entry.

package.json
main
// /jQuery/package.json
{
    "main":"./jQuery/a.js"
}

In this way, we reprinted and found that the results were successfully printed.

Therefore, we can summarize the < strong > search specification of the file module < / strong >:

  • In the latest node version, the default is to find the file with the same name first. If not, try adding. JS and. JSON files. If it still doesn’t exist, it will find the folder with the same name (as a package). After finding it, it will first find the package.json file. If not, it will continue to look for the index.js file. If it still does not exist, an error will be reported.
  • In the old version of node, if there is package.json, the package with the same name will be found first. If there is no package.json, it will find the file with the same name.
// 新版node查找流程
│
└───┤ 同名文件
 │	
 │──┤ 同名.js / 同名.json
 │
 │──┤ 同名文件夹
    │
    │───┤ pageage.json
    │
    └───┤ index.js
   
 都没找到,则抛出一个错误。

This is the custom module lookup specification of node. We may encounter this problem during our usual interview.

For example, we can introduce node’s module lookup mechanism in this way. (at present, few people ask about the search mechanism of the old version of node. They usually ask about the difference between the two search mechanisms. Those interested can consult relevant materials.)

This article is created by < strong > Mo Xiaoshang < / strong >. If there are any problems and mistakes in the article, you are welcome to correct and communicate.
You can also pay attention to my personal site, blog Garden and nuggets. I will upload my articles to these platforms synchronously after they are output.
Finally, thank you for your support!

This article is created by < strong > Mo Xiaoshang < / strong >. If there are any problems and mistakes in the article, you are welcome to correct and communicate.
You can also pay attention to my personal site, blog Garden and nuggets. I will upload my articles to these platforms synchronously after they are output.
Finally, thank you for your support!