关于node.js的模块查找顺序(require.resolve())
2014-08-28 15:37
811 查看
前几天社团群里有人问了阿里秋季前端笔试的一道题,想起来以前在官方文档看到过查找模块的算法,干脆自己写一写……
官方的require.resolve实现在这里. 因为我只是想看看查找过程,所以就直接把会被找的路径console.log出来看看而已。代码放在了我的github gist上:https://gist.github.com/joyeec9h3/74262a250b3e880c7fd4
结果如下
简单来说,如果是require('x')这样开头不是相对or绝对地址符号,尾巴也没说是.js或者.json的,就当做模块来找。先找是不是core module,然后一级一级向上看node_modules文件夹,每一级的node_modules先看里面是否有basename为所找的文件,再看是否有模块名文件夹下package.json的main标明的文件,然后不死心地看看模块名文件夹下有没有index.js和index.node。最后找不到的话,还要搜一遍全局环境,比如$HOME/.node_modules/什么的。
官方的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和 exports
- node.js require 加载自己的模块
- Angular Material串串学客户端开发 2 - Node.js模块加载机制Require()
- 关于node.js require获取绝对路径的问题
- node.js使用require()函数加载模块
- node.js中模块,require
- Node.js模块 require和 exports
- Node.js模块 require和 exports
- node.js require 实现机制初窥;pomelo代码分析5----------- pomel-loader模块
- Node.js模块 require和 exports
- Node.js模块 require和 exports
- Node.js中的require.resolve方法使用简介
- Node.js模块加载顺序
- Node.js模块 require和 exports
- nodejs require本地模块的一些细节笔记
- [ 转]Node.js模块 require和 exports
- node.js使用require()函数加载模块
- 详解Node.js中path模块的resolve()和join()方法的区别
- Node.js错误之关于formidable模块引用失败
- node.js模块,require,underscore