nodejs 初入

來源:互聯網
上載者:User

標籤:blog   http   ar   使用   java   sp   strong   檔案   div   

nodejs 模組路徑

1.內建模組 

  如果傳遞給require函數的是NodeJS內建模組名稱,不做路徑解析,直接返回內部模組的匯出對象,例:require(‘http‘)。

2. nodejs  支援(/)或盤符(C:)開頭的絕對路徑,也支援./開頭的相對路徑。

 

3.nodejs還支援第三種路徑 模組路徑  node_modules ,寫法類似foo/bar , 載入的可以使檔案也可以是包,主要是用來存放npm模組。

   例:在某個模組的絕對路徑是/home/user/app.js,在該模組中使用require(‘foo/bar‘)方式載入模組時,則NodeJS依次嘗試使用以下路徑

/home/user/node_modules/foo/bar    //目前的目錄下的node_modules /home/node_modules/foo/bar           //父目錄下的node_modules my /node_modules/foo/bar    //沿路徑向上逐級遞迴,知道根目錄下的 node_modules 目錄

4.NODE_PATH環境變數 

  NodeJS允許通過NODE_PATH環境變數來指定額外的模組搜尋路徑,  一般情況下,自訂配置的時候 ,node_path指向 npm設定的全域安裝的目錄下的node_modules 

  例:

 NODE_PATH=/home/user/lib:/home/lib

windows 使用分好  linux使用頓號 

當使用require(‘foo/bar‘)的方式載入模組時,則NodeJS依次嘗試以下路徑。

/home/user/lib/foo/bar /home/lib/foo/bar

  

nodejs 初入

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.