node加载模块顺序

当require('xxx')一个模块时,
  • 优先加载核心模块;
  • 以路径形式加载文件模块:
  1. 如果显示指定文件模块扩展名,则在该路径下查找该模块是否存在; 
  2. 如果未显示指定文件模块扩展名,则在该路径下,依次查找以.js、.json、.node为扩展名的模块是否存在;
  • 既不是核心模块,又不是以路径形式表示的模块:
表示该模块一个位于各级node_modules目录的已安装模块(全局安装或局部安装),则首先在当前目录的node_modules目录中查找该模块是否存在,若不存在,则继续在其父目录的node_modules目录中查找,反复执行这一过程,直到根目录为止。
  1. 从module path数组中取出第一个目录作为查找基准。
  1. 直接从目录中查找该文件,如果存在,则结束查找。如果不存在,则进行下一条查找。
  1. 尝试添加.js、.json、.node后缀后查找,如果存在文件,则结束查找。如果不存在,则进行下一条。.js件会以文本格式的JavaScript脚本文件解析,.json文件会以JSON格式的文本文件解析,.node文件会以编译后的二进制文件解析。
  1. 尝试将require的参数作为一个包来进行查找,读取目录下的package.json文件,取得main参数指定的文件。
  1. 尝试查找该文件,如果存在,则结束查找。如果不存在,则进行第3条查找。
  1. 如果继续失败,则取出module path数组中的下一个目录作为基准查找,循环第1至5个步骤。
  1. 如果继续失败,循环第1至6个步骤,直到module path中的最后一个值。
  2. 如果仍然失败,则抛出异常。
如果想得到 require 命令加载的确切文件名,使用 require.resolve() 方法。 require.resolve('server')

当require(‘othermodule ’)时:
查看node安装目录,判断是不是核心模块
---------------------------------------
check /usr/local/lib/node/bar.js
---------------------------------------
当前目录:
---------------------------------------
check /home/somebody/node_modules/othermodule
check /home/somebody/node_modules/othermodule.js
check /home/somebody/node_modules/othermodule.json
check /home/somebody/node_modules/othermodule.node
---------------------------------------
if / home/somebody/node_modules/othermodule/package.json exists
check /home/somebody/node_modules/othermodule/package.json[main]
---------------------------------------
if /home/somebody/node_modules/othermodule/index.js exists
check /home/somebody/node_modules/othermodule/index.js
---------------------------------------
if /home/somebody/node_modules/othermodule/index.node exists
check /home/somebody/node_modules/othermodule/index.node
---------------------------------------

父级目录:
check /home/node_modules/othermodule
check /home/node_modules/othermodule.js
check /home/node_modules/othermodule.json
check /home/node_modules/othermodule.node
---------------------------------------
if /home/node_modules/othermodule/package.json exists
check /home/node_modules/othermodule/package.json[main]
---------------------------------------
if /home/node_modules/othermodule/index.js exists
check /home/node_modules/othermodule/index.js
---------------------------------------
if /home/node_modules/othermodule/index.node exists
check /home/node_modules/othermodule/index.node
---------------------------------------

根目录:
check /node_modules/othermodule
check /node_modules/othermodule.js
check /node_modules/othermodule.json
check /node_modules/othermodule.node
---------------------------------------
if /node_modules/othermodule/package.json exists
check /node_modules/othermodule/package.json[main]
---------------------------------------
if /node_modules/othermodule/index.js exists
check /node_modules/othermodule/index.js
---------------------------------------
if /node_modules/othermodule/index.node exists
check /node_modules/othermodule/index.node
---------------------------------------

由环境变量指定的$NODE_PATH路径中查找(全局安装的模块):
for each $PATH in $NODE_PATH

---------------------------------------
if $PATH/package.json exists
check $PATH/package.json[main]
---------------------------------------
if $PATH/index.js exists
check $PATH/index.js
---------------------------------------
if $PATH/index.node exists
check $PATH/index.node
---------------------------------------
if $HOME/.node_modules/package.json exists
check $HOME/.node_modules/package.json[main]
---------------------------------------
if $HOME/.node_modules/index.js exists
check $HOME/.node_modules/index.js
---------------------------------------
if $HOME/.node_modules/index.node exists
check $HOME/.node_modules/index.node
---------------------------------------
if $HOME/.node_libraries/package.json exists
check $HOME/.node_libraries/package.json[main]
---------------------------------------
if $HOME/.node_libraries/index.js exists
check $HOME/.node_libraries/index.js
---------------------------------------
if $HOME/.node_libraries/index.node exists
check $HOME/.node_libraries/index.node
---------------------------------------
if $PREFIX/lib/node/package.json exists
check $PREFIX/lib/node/package.json[main]
---------------------------------------
if $PREFIX/lib/node/index.js exists
check $PREFIX/lib/node/index.js
---------------------------------------
if $PREFIX/lib/node/index.node exists
check $PREFIX/lib/node/index.node

$HOME 为当前用户的根目录, $PREFIX 通过 node_prefix 来配置。

参考:https://www.cnblogs.com/joyeecheung/p/3941705.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值