Node.js教程 一文掌握模块查找顺序(require.resolve())
小作 2018-02-10 来源 :网络 阅读 985 评论 0

摘要:写这篇Node.js教程是因为阿里秋季前端笔试的一道题,想起来以前在官方文档看到过查找模块的算法,干脆自己写一写……

写这篇Node.js教程是因为阿里秋季前端笔试的一道题,想起来以前在官方文档看到过查找模块的算法,干脆自己写一写……


官方的require.resolve实现在这里. 因为我只是想看看查找过程,所以就直接把会被找的路径console.log出来看看而已。

 

结果如下


---------------------------------------

        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/什么的。


希望这篇文章可以帮助到你。总之,同学们,你想要的职坐标IT频道都能找到!


本文由 @小作 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程