Commonjs 模块化加载原理解析
Commonjs 是在 node 中引入和导出模块的一套模块化规范,使用时通过 require 引入模块,通过 module.exports/exports 导出模块内容。
在 nodejs 中,模块可分为三类
- 内置模块:node 内置的一些核心模块,比如:path、fs
- 用户模块:用户项目里写的 js 文件
- 第三方模块:node_modules 下的模块
在 node 中,导入模块时,每个模块都会被包一层模块加载函数, 这个函数有多个参数分别是 exports、require、module、__filename、__dirname