关于node.js的模块查找顺序(require.resolve())
前几天社团群里有人问了阿里秋季前端笔试的一道题,想起来以前在官方文档看到过查找模块的算法,干脆自己写一写……
官方的require.resolve实现在这里. 因为我只是想看看查找过程,所以就直接把会被找的路径console.log出来看看而已。代码放在了我的github gist上:https://gist.github.com/joyeec9h3/74262a250b3e880c7fd4
结果如下
---------------------------------------
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
--------------------------------------- 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
简单来说,如果是require('x')这样开头不是相对or绝对地址符号,尾巴也没说是.js或者.json的,就当做模块来找。先找是不是core module,然后一级一级向上看node_modules文件夹,每一级的node_modules先看里面是否有basename为所找的文件,再看是否有模块名文件夹下package.json的main标明的文件,然后不死心地看看模块名文件夹下有没有index.js和index.node。最后找不到的话,还要搜一遍全局环境,比如$HOME/.node_modules/什么的。
关于node.js的模块查找顺序(require.resolve())的更多相关文章
- Node.js:模块系统
ylbtech-Node.js:模块系统 1.返回顶部 1. Node.js模块系统 为了让Node.js的文件可以相互调用,Node.js提供了一个简单的模块系统. 模块是Node.js 应用程序的 ...
- Node.js DNS 模块
Node.js DNS 模块用于解析域名.引入 DNS 模块语法格式如下: var dns = require("dns") 方法 序号 方法 & 描述 1 dns.loo ...
- 【node.js】模块系统、函数
为了让Node.js的文件可以相互调用,Node.js提供了一个简单的模块系统. 一个 Node.js 文件就是一个模块,这个文件可能是JavaScript 代码.JSON 或者编译过的C/C++ 扩 ...
- Node.js:模块系统、函数
为了让Node.js的文件可以相互调用,Node.js提供了一个简单的模块系统. 模块是Node.js 应用程序的基本组成部分,文件和模块是一一对应的.换言之,一个 Node.js 文件就是一个模块, ...
- Node.js之模块机制
> 文章原创于公众号:程序猿周先森.本平台不定时更新,喜欢我的文章,欢迎关注我的微信公众号. ,使用pg_pathman则支持hash分区. 从性能角度,目前最 ...
- vs2003一查找就卡死了
Visual Studio 2003一查找就卡死了.解决办法如下: win7 32位下 解决方法:找到VS2003的安装目录,修改"...\Microsoft Visual Studio . ...
- 在使用Hibernate save()方法的时候 报错: org.hibernate.exception.ConstraintViolationException:could not perform addBath
org.hibernate.exception.ConstraintViolationException:could not perform addBath 错误可能原因:实体属性的值与数据库字段类型 ...
- Qt如何获得窗口的几何信息(Window Geometry)
一个窗口除去窗框以后,余下部分称为客户区域.获得窗口的几何信息(坐标,长宽等)有以下两组命令: 1. 包括窗框(即整个窗口):x(), y(), frameGeometry(), pos(), mo ...
- What are the advantages of different classification algorithms?
What are the advantages of different classification algorithms? For instance, if we have large train ...
- BestCoder Round #40 解题报告
这场是第一场没有米的BC... 大概也是想震一震那些一听说没米了就不打BC的人吧 这次的题目质量比以往高了许多 (然而我并没有打这一场BC 但是今天下午到现在做的过程中真的学到了不少知识呢 A题略水. ...
- 【BZOJ】2553: [BeiJing2011]禁忌 AC自动机+期望+矩阵快速幂
[题意]给定n个禁忌字符串和字符集大小alphabet,保证所有字符在集合内.一个字符串的禁忌伤害定义为分割能匹配到最多的禁忌字符串数量(一个可以匹配多次),求由字符集构成的长度为Len的字符串的期望 ...
- Android SDK的安装与环境变量的配置
配置Andriod环境变量前提是要先安装好JAVA环境 1.下载Android SDK,点击安装,放在任意不含空格.特殊符号和中文的路径即可. 2.默认路径安装后,安装完成,开始配置环境变量. 3.打 ...
- sniffer简单使用
跟wireshark类似. 只是说显示的容易忘记所以丢张图记录一下. 该工具还是很坑爹的,不是比赛要用到所以都不是很想弄.一般机器运行不起来.不是蓝屏就是装了运行不了各种闪退,找了学校一台内网服务器才 ...
- 用C#实现对MSSqlServer数据库的增删改查---Server层(WaterLevelRecordServer.cs、DeviceRecordServer.cs)
抛开现实的残酷与无奈,对技术孜孜不倦的追求,从专注到执着,从疯狂到忘我,始终坚信代码可以改变世界,更能改变自己的人生. WaterLevelRecordServer.cs using System; ...