Analysis of Node module mechanism
这篇文章主要介绍了关于Node模块机制的解析,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
模块机制
一、CommonJS出现背景
js实现编写后端程序的不足之处
1. 没有模块系统(js一个先天不足就是模块功能)
2. ECMAScript仅仅定义了js的核心库,但是对于文件系统、IO系统等却没有标准的API。HTML5虽然在一直致力于推进标准化,但是这些标准耶都是前端的。
3. 没有标准接口,没有定义过服务器或者数据库的接口。
4. 缺乏包管理系统,没有自动安装和管理依赖的能力。
而CommonJS的出现正好弥补了没有标准的这一个缺点。
CommonJS
规范了 模块
、二进制
、Buffer
、二进制
、I/0
,进程环境
、文件系统
、web服务器网管接口
、包管理等
。
二、CommonJS和Node的关系
Node的出现离不开CommonJS规范的影响,而CommonJS能以一种独寻常的姿态出现在各大公司的代码中,离不开Node优异的表现。
三、CommonJS的模块规范
1. 模块引入
1 |
|
2. 模块导出
1 2 3 4 |
|
1 2 3 4 5 6 |
|
3. 模块标识
模块标识就是require()
的括号中的参数,必须是小驼峰结构,可以是相对路径也可以是绝对路径,还可以没有后缀名。
四、Node的模块实现
node 的模块实现实际上是借鉴了CommonJS的部分,并不是全部照搬,而且也增加了一些自己需要的东西进去。
1. 模块引入
node中模块引入需要经过:
1 2 3 |
|
2. 模块分类
1 2 |
|
3. 核心模块
1 |
|
4. 文件模块
1 |
|
5. 缓冲加载
1 |
|
6. 路径分析和文件定位
模块标识符分析
核心模块
1 |
|
路径形式的文件模块
1 2 |
|
自定义模块
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
文件定位
文件定位中还需要注意的包括有文件扩展名
、目录
、包的处理
。
node 的模块引入的时候是可以不写扩展名的,node会按照js
json
node
的顺序来分析。依次尝试。由于尝试使用的是node中fs模块的同步文件查找,因此可能会导致阻塞情况发生,因此这里我们需要注意两个小技巧了:
小技巧:
1. json
node
文件最好加扩展名
2. 同步配合缓冲可以环节Node单线程阻塞调用的缺陷
另外,如果没有找到对应的文件,确实找到了一个目录,那么将会将其当作是一个包来处理了。
如何在这个包下找到我们需要引入的入口文件对呢?
1. 首先找是否含有package.json,如果有,则分析它的main属性,找到main属性对应的那个文件。
2. 如果没有package.json或者是main解析失败了,那么就找文件名为index的文件,依次从index.js
index.json
index.node
查找。
3. 如果在该目录下依旧没有找到,那么就查找写一个匹配的目录,如果仍然没有找到,那么就报错了。
6. 模块编译
node中每一个模块都是一个对象,当定位到一个文件的时候,node就会将其包装成一个module
对象,然后根据不同的文件名,其载入方法也不同的。
js文件: 通过fs的同步读取文件来执行
json文件,通过fs同步读取文件,用Json.parse()来得到对象,将其赋值给Module.exports
node文件,这事c/c++编写的扩展文件,通过Process.dlopen()方法来加载执行。(不需要编译)
7. module.exports和exports的区别和联系
exports = module.exports = {};
的区别就和 var a = {}; b = a;
的区别一样.
首先要明确的一点,module
是一个对象 {Object}。
当你新建一个文件,比如mo.js
,文件内容如下:
1 |
|
然后在CMD里执行这个文件node mo.js,就能看到module其实是一个Module实例,你可以这么理解,NodeJS中定义了一个Module类,这个类中有很多属性和方法,exports是其中的一个属性:
1 2 3 4 5 |
|
当每一个文件被执行或者时require的时候,node就会创建一个module实例。var module = new Module();
1 2 3 4 5 |
|
module.exports 其实就是module实例中的module添加方法或者属性。
1 2 3 4 5 6 7 8 |
|
不难看出exports其实就是module.exports的一个引用。
1 2 3 |
|
当你require的时候,返回的就是module.exports的内容。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
总结
改变exports的指向后所添加的exports.xxx都是无效的。因为require返回的只会是module.exports
不能在使用了exports.xxx之后,改变module.exports的指向。因为exports.xxx添加的属性和方法并不存在于module.exports所指向的新对象中。
对于要导出的属性,可以简单直接挂到exports对象上
对于类,为了直接使导出的内容作为类的构造器可以让调用者使用new操作符创建实例对象,应该把构造函数挂到module.exports对象上,不要和导出属性值混在一起
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!
相关推荐:
The above is the detailed content of Analysis of Node module mechanism. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics





This article will give you an in-depth understanding of the memory and garbage collector (GC) of the NodeJS V8 engine. I hope it will be helpful to you!

The Node service built based on non-blocking and event-driven has the advantage of low memory consumption and is very suitable for handling massive network requests. Under the premise of massive requests, issues related to "memory control" need to be considered. 1. V8’s garbage collection mechanism and memory limitations Js is controlled by the garbage collection machine

Choosing a Docker image for Node may seem like a trivial matter, but the size and potential vulnerabilities of the image can have a significant impact on your CI/CD process and security. So how do we choose the best Node.js Docker image?

The file module is an encapsulation of underlying file operations, such as file reading/writing/opening/closing/delete adding, etc. The biggest feature of the file module is that all methods provide two versions of **synchronous** and **asynchronous**, with Methods with the sync suffix are all synchronization methods, and those without are all heterogeneous methods.

Node 19 has been officially released. This article will give you a detailed explanation of the 6 major features of Node.js 19. I hope it will be helpful to you!

How does Node.js do GC (garbage collection)? The following article will take you through it.

The event loop is a fundamental part of Node.js and enables asynchronous programming by ensuring that the main thread is not blocked. Understanding the event loop is crucial to building efficient applications. The following article will give you an in-depth understanding of the event loop in Node. I hope it will be helpful to you!

How to package nodejs executable file with pkg? The following article will introduce to you how to use pkg to package a Node project into an executable file. I hope it will be helpful to you!
