關於node.js的模塊查找順序(require.resolve())

前幾天社團羣裏有人問了阿里秋季前端筆試的一道題,想起來之前在官方文檔看到過查找模塊的算法,乾脆本身寫一寫……html

官方的require.resolve實如今這裏. 由於我只是想看看查找過程,因此就直接把會被找的路徑console.log出來看看而已。代碼放在了個人github gist上:https://gist.github.com/joyeec9h3/74262a250b3e880c7fd4前端

 

結果以下node

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

相關文章
相關標籤/搜索